Launch Android application from Library project

2019-07-20 18:14发布

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条回答
▲ chillily
2楼-- · 2019-07-20 19:01

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

查看更多
登录 后发表回答