StartForeground坏的错误通知 [英] StartForeground Bad Notification Error

查看:6142
本文介绍了StartForeground坏的错误通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用whil​​e循环,以更新通知每一秒。但是在2.3.3和放大器;下面,它与这些logcat的错误崩溃:

I am trying to use a while loop in order to update a notification every second. However on 2.3.3 & below, it crashes with these logcat errors:

08-14 07:30:17.394: E/AndroidRuntime(501): FATAL EXCEPTION: main
08-14 07:30:17.394: E/AndroidRuntime(501): 
android.app.RemoteServiceException: Bad notification for startForeground: 
java.lang.IllegalArgumentException: contentIntent required: 
pkg=com.package.name id=77 
notification=Notification(vibrate=null,sound=null,defaults=0x4,flags=0x40)

问题是,即使当我检查Build.VERSION中,code崩溃,并同logcat的错误:

The problem is even when I check Build.VERSION, the code crashes with the same logcat errors:

if (isRunning) {
        n.defaults = Notification.DEFAULT_LIGHTS;
        while (!RecordTimerTask.isRunning && isRunning) {
            long now = System.currentTimeMillis() - startTime;
            n.setLatestEventInfo(getApplicationContext(), getResources()
                    .getString(R.string.notify_title),
                    getDurationBreakdown(now), null);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                startForeground(FOREGROUND_ID, n);
            else 
                notify.notify(ID, n);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

这似乎是运行2.3.3与放大器设备;下面不喜欢空通知意图。
我不明白的是为什么我得到一个startForeground错误logcat中时,它从来不叫?

It seems like devices running 2.3.3 & below don't like the null notification intents. What I don't understand is why I get a logcat error about startForeground when it's never called?

推荐答案

问题是你叫的 setLatestEventInfo 的使用为的PendingIntent null参数。这适用于Android ICS但不能在早期版本的...

The problem is you call setLatestEventInfo with a null argument for the pendingIntent. This works on Android ICS but not on earlier versions...

对了的 <一的文档的外观href=\"http://developer.android.com/reference/android/app/Notification.html#setLatestEventInfo%28android.content.Context,%20java.lang.CharSequence,%20java.lang.CharSequence,%20android.app.PendingIntent%29\"相对=nofollow> setLatestEventInfo

下面是你的code应该如何看(如果你是从一个服务推动这些通知):

Here is how your code should look (if you are pushing these notifications from a service):

        // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, LocalServiceActivities.Controller.class), 0);

    // Set the info for the views that show in the notification panel.
    notification.setLatestEventInfo(this, getText(R.string.local_service_label),
                   text, contentIntent);

code从的Andr​​oid官方文档引用。

另请参阅<一个href=\"http://stackoverflow.com/questions/11265942/java-lang-illegalargumentexception-contentintent-required/12138517#12138517\">this对于在计算器上类似的问题。

Also see this for a similar issue on StackOverflow.

这篇关于StartForeground坏的错误通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆