0
アラームマネージャを使用して、受信者メソッドで通知を呼び出しています。私はそれにいくつかのデバッグコードを入れて、そのプロセスがそこに入っていないことが分かった。ここでBroadcastReceiveのonReceiveメソッドに取り込めません
は私のコードです:
たManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Menu">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ContentInput"
/>
<receiver
android:process=":remote"
android:name=".NoticationBroadcast"/>
</application>
Menu.java(メインクラス)
Button noti = (Button) findViewById(R.id.noti);
noti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(ErrorTag, "before onclick");
scheduleNotification(getNotification("ggggg"),2000);
Log.e(ErrorTag, "after onclick");
}
});
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Scheduled Notification");
builder.setContentText(content);
builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
Log.e(ErrorTag, "finish get notificattion");
return builder.build();
}
private void scheduleNotification(Notification notification, int delay) {
Intent notificationIntent = new Intent(this, NoticationBroadcast.class);
notificationIntent.putExtra(NoticationBroadcast.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NoticationBroadcast.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Log.e(ErrorTag, "after getBroadcast");
long futureInMillis = SystemClock.elapsedRealtime() + delay;
Calendar calendar = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.e(ErrorTag, "after alarm");
}
レシーバクラス
public class NoticationBroadcast extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
@Override
public void onReceive(Context context, Intent intent) {
Log.e("ERROR", "beginning of on receive");
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
Log.e("ERROR", "end of on receive");
}
}
編集:
私はそれが完璧に働いている
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5000), pendingIntent);
にコード
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
の行を変更しました。しかし、私はなぜそれが分からない。誰かが何が起こったのか分かっているなら、教えてください。ありがとう!
私はあなたのアプローチを考えます間違っている場合は私を訂正してください。あなたは動作していないかもしれない放送受信機に通知オブジェクトを送ります。通知のためにサービスを使用してください。 –
あなたはgoogle上の通知で簡単なアラームマネージャの多くのチュートリアルを得ることができます。 –
私はそれを解決したと思います。しかし私は私が何をしたのかわからない:O – richardrun