【问题标题】:Activity is not launching after clicking on Notification单击通知后活动未启动
【发布时间】:2014-03-11 11:02:20
【问题描述】:

我正在BrodCastReceiver 上做一个程序。因此,在我的程序中,当收到任何事件时,它会显示通知,但在单击相同的通知后,它不会启动指定的活动。

这是我的代码

public void onReceive(Context context, Intent i) {
    // TODO Auto-generated method stub
    Intent intent=new Intent(context, MainActivity.class);

    Toast.makeText(context, "Priority 2", Toast.LENGTH_SHORT).show();

    mBuilder=new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!")
                .build();

     NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

     mNotificationManager.notify(1, mBuilder);
     PendingIntent.getBroadcast(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

     mNotificationManager.notify(1, mBuilder);
}

【问题讨论】:

  • 您是否将待处理的 Intent 设置为 builder

标签: android android-intent notifications


【解决方案1】:
public void onReceive(Context context, Intent i) {
// TODO Auto-generated method stub
Intent intent=new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, intent.FLAG_ACTIVITY_NEW_TASK);
Toast.makeText(context, "Priority 2", Toast.LENGTH_SHORT).show();

mBuilder=new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My notification")
            .setContentText("Hello World!").setContentIntent(pendingIntent);
            .build();

 NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

 mNotificationManager.notify(1, mBuilder);
 PendingIntent.getBroadcast(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

 mNotificationManager.notify(1, mBuilder);
}

setContentIntent(pendingIntent);您需要在创建 NotificationManager 时进行设置。

【讨论】:

  • 上述代码中缺少PendingIntent PendingIntent pendingIntent= PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
【解决方案2】:

您应该添加一个待处理的意图,该意图将响应点击事件以打开相应的 活动

试试这个:

public void onReceive(Context context, Intent i) {
    // TODO Auto-generated method stub
    Intent intent=new Intent(context, MainActivity.class);
           intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent   pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Toast.makeText(context, "Priority 2", Toast.LENGTH_SHORT).show();

    mBuilder=new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentIntent(pendingIntent)
                .setContentText("Hello World!")
                .build();

     NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

     mNotificationManager.notify(1, mBuilder);



}

希望对你有帮助

【讨论】:

    【解决方案3】:
    Intent intent=new Intent(context, MainActivity.class);
    pendingIntent = PendingIntent.getBroadcast(context, 0, intent, intent.FLAG_ACTIVITY_NEW_TASK);
    mBuilder=new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My notification")
            .setContentText("Hello World!").setContentIntent(pendingIntent);
            .build();
     NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(1, mBuilder)
    

    首先初始化 pendingIntent 并在构建期间设置它。

    【讨论】:

      猜你喜欢
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 2014-02-10
      相关资源
      最近更新 更多