Launch Android application from Library project

こ雲淡風輕ζ 提交于 2019-12-13 16:27:11

问题


I am developing the PushNotification for android application in Android Library. I am unable to launch the android application while clicking the push notification message. I am unable to fetch the android application class in library project for launch in generatePushNotification() method. The following is the code snippet from library project.

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name); 
 // Here I am getting the android application context as sActiveContext
    Intent notificationIntent = new Intent(context,  "need to launch the android application main activity");
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.defaults |= Notification.DEFAULT_SOUND;

    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);      

}

How can I launch the android application from the Library Project.?


回答1:


if your problem is that the activity you wish to provide to the intent is not recognized in the library project, you can use the packageManager.getLaunchIntentForPackage() to get intent to start activity from this package which have CATEGORY_LAUNCHER attribute

String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);

then you could replace new Intent(context, "need to launch the android application main activity") with launchIntent



来源:https://stackoverflow.com/questions/18843118/launch-android-application-from-library-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!