0

一定間隔でゲームからの通知を投稿しようとしています。BroadcastReciever クラスの onReceive() メソッドから PostNotification() 関数を呼び出して、ゲーム開始の 1 分後に通知を投稿します。

ブロードキャストレシーバー

public class NotificationReciever extends BroadcastReceiver
{

private static int count=0;

@Override
public void onReceive(Context context, Intent intent)
{
    try 
    {
         Bundle bundle = intent.getExtras();
         String message = bundle.getString("message");
         int id = bundle.getInt("id");
         PostNotification(message, id);
    }
    catch (Exception e) 
    {
         e.printStackTrace();
    }
    wl.release();
}

public void PostNotification(String notif, int id)
{
    Notification notify=new Notification(R.drawable.icon,
            notif,
            System.currentTimeMillis());
Intent intent = new Intent(MyUtil.getInstance().context, MyActivity.class);
        PendingIntent i=PendingIntent.getActivity(MyUtil.getInstance().context, 0, intent, 0);
        notify.setLatestEventInfo(MyUtil.getInstance().context, "Title", notif, i);  
        MyActivity.notifyMgr.notify(id, notify);
        }
    }
}

MyActivity の onCreate() から ScheduleNotification() を呼び出しています

public void ScheduleNotification()
{
     Calendar cal = Calendar.getInstance();
     Intent intent = new Intent(MyUtil.getInstance().context, NotificationReciever.class);
     intent.putExtra("id", NOTIFY_ID);
     intent.putExtra("message", "message");
     PendingIntent sender = PendingIntent.getBroadcast(MyUtil.getInstance().context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
     am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}

しかし、通知を受信せず、logcat に次のエラーが表示されます

  01-11 19:28:32.455: W/System.err(20661): java.lang.NullPointerException
01-11 19:28:32.475: W/System.err(20661):    at android.content.ComponentName.<init>(ComponentName.java:75)
01-11 19:28:32.475: W/System.err(20661):    at android.content.Intent.<init>(Intent.java:2893)
01-11 19:28:32.475: W/System.err(20661):    at com.games.TestGame.NotificationReciever.PostNotification(NotificationReciever.java:41)
01-11 19:28:32.475: W/System.err(20661):    at com.games.TestGame.NotificationReciever.onReceive(NotificationReciever.java:27)

通知の意図を作成するときに何か間違ったことをしていることを知っています。アクティビティから直接呼び出した場合は正しく通知を受け取りますが、アラームを介して呼び出すと問題が発生します

インテント インテント = 新しいインテント(MyUtil.getInstance().context, MyActivity.class);

誰でも私が間違っているところを教えてください。

4

2 に答える 2

1

使用するNotificationコンストラクターのwhenパラメーターは、単に表示を目的としています。通知の表示が遅れることはありません。

アラームを使ってみませんか?ただし、インテントのみを受け入れます。

于 2013-01-11T10:32:09.643 に答える
1

それは完全な理解の間違いです。の詳細を見てくださいpublic Notification (int icon, CharSequence tickerText, long when)。以下のようになります。

icon ステータスバーに表示するアイコンのリソースID。

tickerText通知が最初にアクティブになったときにステータスバーを流れるテキスト。

時間フィールドに表示する時間System.currentTimeMillisタイムベース。

つまり、通知の時間ではなく、通知マッサージで表示するためだけのものです。将来特定の時間に通知を送信する場合は、その時間にAlermManagerを設定する必要があります。AlermManagerはBroadcastReceiverを呼び出します。したがって、BroadcastReceiverも作成する必要があります。この場合、通知を設定する必要があります。このリンクを使用できます。

于 2013-01-11T10:34:01.830 に答える