我是Android编程的初学者,我需要你的帮助。 当我点击Androidnotification时,我想开始活动“通知”。我在Stackoverflow上找到这个主题的东西不起作用。 所以我希望你能帮助我。
Bootreceiver:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.logo_icon);
builder.setContentTitle("Warnung");
builder.setContentText("Stromzähler 1 braucht 90% zu viel Strom");
Intent in = new Intent(context, Notification.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(in);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
班级通知:
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.notification);
loadBackHome();
loadAllNotifications();
}
答案 0 :(得分:0)
这是我的代码。
public void onReceive(Context context, Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Title....")
.setContentText("Bla bla bla~~~~")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pIntent);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = notificationBuilder.build();
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationManager.notify(0, notification);
}