[英]Launching an activity when a notification icon is clicked on the status bar?
我正在使用警报管理器设置警报。 当它关闭时,接收器将启动此服务。 从该服务使用以下代码进行通知。 我想在单击通知时进行一项活动,但是当我单击通知图标时,什么也没有发生。
它仅显示该圆正在移动,就好像正在加载某些东西。
package com.rythmal.alarmtest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class AlarmProcess extends Service {
private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private Notification myNotification;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ht_icon;
CharSequence tickerText = "Rythmal";
long when = System.currentTimeMillis();
myNotification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence notificationTitle = "My notification";
CharSequence notificationText = "Hello World!";
Intent notificationIntent = new Intent(this,com.rythmal.alarmtest.ResultRun.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, 0);
myNotification.defaults |= Notification.DEFAULT_SOUND;
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle,notificationText, pendingIntent);
mNotificationManager.notify(MY_NOTIFICATION_ID, myNotification);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
嘿,在我清理完项目后,它一下子就起作用了。
我已经查看了您的通知代码。 一切看起来都正确。
您的意思是“但是当我单击通知图标时,什么都没有发生。它只是显示此圆圈在移动,好像正在加载东西而已”。 我问,因为“它只是显示这个圆圈”绝对意味着它在做某事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.