2015-05-02 63 views
0

我有需要时,一些事件发生如何解决Android通知问题?

package com.octoplus.client.services; 

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; 

import com.octoplus.client.MainActivity; 
import com.octoplus.client.R; 

import java.util.concurrent.TimeUnit; 

public class notif extends Service { 
    public notif() { 
    } 
    NotificationManager nm; 
    @Override 
    public IBinder onBind(Intent intent) { 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    } 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     sendNotif(); 
     return super.onStartCommand(intent, flags, startId); 
    } 

    void sendNotif() { 
     // 1-я часть 
     Notification notif = new Notification(R.drawable.ic_launcher, "We create gif", System.currentTimeMillis()); 
     // ставим флаг, чтобы уведомление пропало после нажатия 
     notif.flags |= Notification.FLAG_AUTO_CANCEL; 
     // отправляем 
     nm.notify(1, notif); 
/* 
     // 3-я часть 
     Intent intent = new Intent(this, MainActivity.class); 
     // intent.putExtra(MainActivity.FILE_NAME, "somefile"); 
     PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

     // 2-я часть 
     notif.setLatestEventInfo(this, "Notification's title", "Notification's text", pIntent); 
*/ 


    } 

} 

写在状态栏上的通知我称之为利用这个代码

startService(new Intent(MainActivity.this, notif.class)); 

一个服务,但我做了一些错误,我总是得到这个例外

java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=com.octoplus.client/.services.notif }: java.lang.IllegalArgumentException: contentView required: pkg=com.octoplus.client id=1 notification=Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 kind=[null]) 
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2721) 
    at android.app.ActivityThread.access$1900(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5103) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.IllegalArgumentException: contentView required: pkg=com.octoplus.client id=1 notification=Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 kind=[null]) 
    at android.os.Parcel.readException(Parcel.java:1435) 
    at android.os.Parcel.readException(Parcel.java:1385) 
    at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:320) 
    at android.app.NotificationManager.notify(NotificationManager.java:136) 
    at android.app.NotificationManager.notify(NotificationManager.java:109) 
    at com.octoplus.client.services.notif.sendNotif(notif.java:41) 
    at com.octoplus.client.services.notif.onStartCommand(notif.java:31) 
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2704) 
    ... 10 more 

我该如何解决它?

+0

你在哪里调用这个startService(新的意图(MainActivity.this,notif.class)); – apk

+0

从其他活动活动 – Vahagn

+0

您是否在清单文件中声明了此服务? – apk

回答

0

发现您并未调用Notification.setLatestEventInfo()。这可能会导致崩溃尝试取消注释它