2

通知を毎秒更新するためにwhileループを使用しようとしています。ただし、2.3.3以下では、次のlogcatエラーでクラッシュします。

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をチェックしても、同じlogcatエラーでコードがクラッシュすることです。

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以下を実行しているデバイスは、null通知インテントを好まないようです。私が理解していないのは、startForegroundが呼び出されないのに、なぜlogcatエラーが発生するのですか?

4

1 に答える 1

4

問題は、pendingIntentの引数をnullにしてsetLatestEventInfoを呼び出すことです。これはAndroidICSで機能しますが、以前のバージョンでは機能しません...

setLatestEventInfoのドキュメントをご覧ください。

コードは次のようになります(サービスからこれらの通知をプッシュする場合)。

        // 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);

コードはAndroidの公式ドキュメントから引用されました。

StackOverflowの同様の問題についてもこちらをご覧ください。

于 2012-08-27T08:42:41.370 に答える