ユーザーがアンドロイドシステム時間を変更したときに、プリファレンスにブール値を保存します。従って、私はマニフェストにACTION_TIME_CHANGEDブロードキャストアクションを追加:アプリケーションが閉じられたときにTIME_SETブロードキャストを受信する
<receiver android:name="test.TimeChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
TimeChangedReceiver)は(BroadcastReceiverを拡張しonReceiveを上書き。このクラスではブール値が保存され、通知が表示されます。アプリが閉じられるまで
public class TimeChangedReceiver extends BroadcastReceiver {
private static final String TAG = "TimeChangedReceiver";
public TimeChangedReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
// store boolean to SharedPreferences
PreferenceUtils.getInstance().storeBoolean(true, PreferenceUtils.CHECK_LICENSE);
// build notification
int icon = android.R.drawable.ic_dialog_alert;
String title = "changed time";
String text = "user changed time";
long when = System.currentTimeMillis();
String timezone = intent.getStringExtra("time-zone");
Notification notification = new Notification(icon, title, when);
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notification_id = (int) System.currentTimeMillis();
Intent notificationIntent = new Intent(context, MainView.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.putExtra("time-zone", timezone);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(context, title, text, contentIntent);
mgr.notify(notification_id, notification);
}
}
すべてが正常に動作している - もう、バックグラウンドで走っていません。
ここでは述べています:
- If you declare the BroadcastReceiver in the Manifest, it will always be active and be called even if the application is closed/stopped
- By putting your BroadcastReceiver in your Manifest, it, by default, is always active.
私はとにかく放送を受信し、ブール値を格納することができますどのように?
私は通知を見る必要はありません。 docsから
'android:enabled =" true "'を 'android:name =" ... "属性の後ろまたは前に' 'の受信者に追加してみてください –
Marat
デフォルトで有効になっています^ –
@Marat [link](https://developer.android.com/guide/topics/manifest/receiver-element.html) デフォルト値は "true"です。それは何も変わりません。 : – maryBlaa