【问题标题】:Launch Android application from Library project从 Library 项目启动 Android 应用程序
【发布时间】:2013-09-21 11:51:50
【问题描述】:

我正在为 Android 库中的 android 应用程序开发 PushNotification。单击推送通知消息时,我无法启动 android 应用程序。 我无法获取库项目中的 android 应用程序类以在 generatePushNotification() 方法中启动。 以下是库项目中的代码 sn-p。

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);      

}

如何从 Library Project 启动 android 应用程序?

【问题讨论】:

    标签: android android-intent push-notification android-library


    【解决方案1】:

    如果您的问题是您希望提供给意图的活动在库项目中未被识别,您可以使用packageManager.getLaunchIntentForPackage() 来获取意图以从此具有CATEGORY_LAUNCHER 属性的包启动活动

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

    那么您可以将new Intent(context, "need to launch the android application main activity") 替换为launchIntent

    【讨论】:

    • 推送通知消息被发送到设备,从通知中打开消息时,必须启动应用程序。这是我的要求。感谢您的回复。我会试试这个代码。
    • 挖掘这个答案,因为它也适用于 Xamarin.Android:Intent launchIntent = Context.PackageManager.GetLaunchIntentForPackage(Application.Context.PackageName);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 2013-05-02
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多