我正在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);
}
答案 0 :(得分: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时设置此项。
答案 1 :(得分:0)
您应该添加一个待处理的intent,它将响应click事件以打开相应的 活性
试试这个:
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);
}
希望有所帮助
答案 2 :(得分:0)
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并在构建期间设置它。