사용자가 Android 시스템 시간을 변경할 때 환경 설정에 부울을 저장하고 싶습니다.앱 종료시 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는 브로드 캐스트 리시버를 확장하고() onReceive를 오버라이드 (override) : 그러므로 나는 매니페스트에 ACTION_TIME_CHANGED 방송 활동을 추가했다. 이 클래스에서 부울이 저장되고 알림이 표시됩니다. 응용 프로그램이 닫힐 때까지
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.
가 어떻게이 어쨌든 방송하고 부울을 저장할받을 수 있습니까?
알림을 볼 필요가 없습니다. 가입일
'android : enabled = "true"'와'android : enabled = "true"를'android : name = "..."'속성 뒤 또는 앞에 추가하십시오. – Marat
기본적으로 사용 가능합니다^ –
@Marat [link] (https://developer.android.com/guide/topics/manifest/receiver-element.html) 기본값은 "true"입니다. 그것은 아무것도 바뀌지 않습니다. : – maryBlaa