Java 用于通知的Android IntentService
我正在尝试创建一个完全独立于活动的服务,并一直在后台运行,以发送有关传入事件的通知。通过在onStartCommand中返回START\u STICKY值,我已经解决了服务终止和活动的问题。它工作得很好,但是我在发送通知时遇到了问题。问题出在setSmallIcon方法中。当我在那里传递对R.drawable.ic_启动器的引用时,我得到了一个错误,即图标==0。有没有办法让它正常工作 这是我的NotificationService.javaJava 用于通知的Android IntentService,java,android,notifications,alarm,Java,Android,Notifications,Alarm,我正在尝试创建一个完全独立于活动的服务,并一直在后台运行,以发送有关传入事件的通知。通过在onStartCommand中返回START\u STICKY值,我已经解决了服务终止和活动的问题。它工作得很好,但是我在发送通知时遇到了问题。问题出在setSmallIcon方法中。当我在那里传递对R.drawable.ic_启动器的引用时,我得到了一个错误,即图标==0。有没有办法让它正常工作 这是我的NotificationService.java public class NotificationS
public class NotificationService extends IntentService {
private static final int UPDATE_TIME = 60 * 1000;
private ArrayList<Group> groups;
public NotificationService() {
super("NotficationService");
// TODO Auto-generated constructor stub
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return START_STICKY;
}
private void showNotification(String name, int remindTime) {
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification n = new Notification.Builder(this)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(pIntent)
.setSmallIcon(android.R.drawable.ic_delete)
.setSound(sound)
.setAutoCancel(true)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
}
@Override
protected void onHandleIntent(Intent intent) {
while(true) {
checkForNotification();
try {
Thread.sleep(UPDATE_TIME);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
公共类NotificationService扩展了IntentService{
私有静态最终整数更新时间=60*1000;
私有ArrayList组;
公共通知服务(){
超级(“通知服务”);
//TODO自动生成的构造函数存根
}
@凌驾
公共IBinder onBind(意图arg0){
//TODO自动生成的方法存根
返回null;
}
@凌驾
public void onCreate(){
super.onCreate();
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
//TODO自动生成的方法存根
返回开始时间;
}
私有void showNotification(字符串名称,整数时间){
意图=新意图();
intent.setClass(this,MainActivity.class);
intent.setFlags(intent.FLAG\u活动\u新任务);
PendingEvent pIntent=PendingEvent.getActivity(this,(int)System.currentTimeMillis(),intent,0);
Uri sound=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知);
通知n=新建通知.Builder(此)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(pIntent)
.setSmallIcon(android.R.drawable.ic_delete)
.setSound(声音)
.setAutoCancel(真)
.build();
NotificationManager NotificationManager=
(通知经理)getSystemService(通知服务);
notificationManager.notify(0,n);
}
@凌驾
受保护的手控无效内容(意图){
while(true){
checkForNotification();
试一试{
睡眠(更新时间);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
尝试使用:
.setSmallIcon(Resources.getSystem().getDrawable(android.R.drawable.ic_delete))
而不是
.setSmallIcon(android.R.drawable.ic_delete)
我认为您对服务的看法是错误的。Intent服务只适用于特殊情况,如有工作要做,然后当没有工作时它会自动退出。文档说您不应该实现OnStartCommand(,int,int))。您必须在服务类中进行扩展以完成长时间运行的工作。我不会使用while(true)在Android中。它会杀死无限循环和递归线程。我想你真正想要的是一个重复的警报,它会在这个更新时间间隔向你的服务发送一个意向。此外,虽然意向服务意味着有很长的使用寿命,但它的onHandleContent()应该很短。它意味着重用服务onCreate()中的对象它不起作用,因为setSmallIcon方法需要一个整数或图标对象,而这里我们有一个可绘制的对象。