私の目標は、ユーザーが選択した時刻に表示される通知を実装することです。 私はすでにService
を試しましたが、これはうまくいきましたが、問題は多くのバックグラウンドプロセス(コードは下にあります)ともう1つはAlarmManager
でしたが、これは正しく動作していないようです。私は最新のよくコード化されたチュートリアルや有用な答えを見つけることができませんでした。Androidカスタムタイム通知サービス/ブロードキャスト/アラームマネージャー
あなたが知っているのは、お友達がWhatsappに電話をかけた場合、電話が切れているか、スリープ状態(画面が消えている)であっても通知が届きます。 あなたのうちの何人かは既にこのようなものを実装していることを確かめてください。
タスクは正確に: 私のアプリは、毎日のユーザに彼のカレンダーで予定されているイベントについて毎日を通知しなければなりません。
例: 彼は13:00(私はいつも24時間形式を使用しています)を選択して、彼の出来事を思い出させます。 - 学校 に子供を運転 - アプリが通知を示し13:00
次の日ジムに行く: は、彼は次の日に2つのイベントを予定し 「あなたは今日のための2つのイベントを!:持っている1)。。 2)... "
通知で情報を入力することは問題ではありません。これはうまく動作し、私はそれをたくさんテストしました。 通知を表示するためにユーザーが選択した時間を監視しているウェルコードのバックグラウンドスレッドのみが欠けています。
はの点に注意してください。
TimerTaskを刻み、intervallは1000ミリ秒(1second)です。確かにこれはコード化されていませんが動作します。 しかし、私はより良い、よりバッテリーに優しいソリューションを探しています。
また私は"android.permission.RECEIVE_BOOT_COMPLETED"と"android.permission.WAKE_LOCK"権限を追加しました。
サービスはとしてフラグが立てられるSTART_STICKY
たManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name=".services.NotificationService" android:exported="false"/>
<receiver android:name=".services.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//Other Activities
</application>
</manifest>
BootReceiver:
public class BootReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, NotificationService.class));
}
}
NotificationService:
public class NotificationService extends Service{
private Calendar currentCalendar, plannedCalendar;
public static final String FILENAME_SETTINGS = "MySettings";
private static final int MODE_PRIVATE = 0;
SharedPreferences data;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
private Timer timer;
private TimerTask timerTask = new TimerTask() {
@Override
public void run() {
data = getSharedPreferences(FILENAME_SETTINGS, MODE_PRIVATE);
int plannedHour = data.getInt("alarm_hour", 0);
int plannedMinute = data.getInt("alarm_minute", 0);
plannedCalendar = Calendar.getInstance(Locale.getDefault());
plannedCalendar.set(Calendar.MINUTE, plannedMinute);
plannedCalendar.set(Calendar.HOUR_OF_DAY, plannedHour);
plannedCalendar.set(Calendar.SECOND, 0);
plannedCalendar.set(Calendar.MILLISECOND, 0);
//Kalender aktualisieren
currentCalendar = Calendar.getInstance(Locale.getDefault());
currentCalendar.setTimeInMillis(System.currentTimeMillis());
//Check ob es 00:00:00 Uhr ist --> Notification wieder erwuenscht!!!
currentCalendar.set(Calendar.SECOND, 0);
currentCalendar.set(Calendar.MILLISECOND, 0);
if(currentCalendar.get(Calendar.SECOND) == 0 && currentCalendar.get(Calendar.MINUTE) == 0 && currentCalendar.get(Calendar.HOUR_OF_DAY) == 0){
SharedPreferences.Editor editor = data.edit();
editor.putBoolean("notification", false);
editor.apply();
}
//Nochmal aktualisieren weil durch den "Check" manipuliert wurde
currentCalendar = Calendar.getInstance(Locale.getDefault());
currentCalendar.setTimeInMillis(System.currentTimeMillis());
if((currentCalendar.getTimeInMillis() >= plannedCalendar.getTimeInMillis()) & !data.getBoolean("notification", false)) {
PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
notification();
SharedPreferences.Editor editor = data.edit();
editor.putBoolean("notification", true);
editor.apply();
wakeLock.release();
}
}
};
@Override
public void onCreate(){
super.onCreate();
timer = new Timer();
timer.schedule(timerTask, 0, 1000);
}
@Override
public void onDestroy() {
try{
timer.cancel();
timerTask.cancel();
}catch (Exception e){
e.printStackTrace();
}
Intent intent = new Intent("Intent");
intent.putExtra("VALUE", "blalal");
sendBroadcast(intent);
}
public void notification(){
Context context = getApplicationContext();
Intent intent = new Intent(context, LiveSelectActivity.class);
PendingIntent pen = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(getString(R.string.hineweisTrainingsGeplant))
.setContentText("Content")
.setContentIntent(pen)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_add_white_24dp);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
「私はすでにサービスを試しましたが、これは動作します」 - Android 6.0以降ではありません。古いデバイスでは、確実に動作することができますが、ユーザはあなたの人生を脅かすでしょう。もう1つはAlarmManagerでしたが、これは正しく機能していないと思われます。特にAPI 19+では " - あなたが使用するものに依存します。 'setAlarmClock()'は信頼できるものです。例えば、 "目覚まし時計"のメタファーに非常に結びついたUI要素があります。 「友だちのテキストでWhatsappに電話をかけると、電話が切れていても寝ていても(画面が消えても)通知を受けることができます。彼らはGoogle Cloud Messaging、AFAIKを使用します。 – CommonsWare
@CommonsWareクイックアドバイスありがとう。私はあらゆる想像的な方法でAlarmManagerを試しました:setExakt()、setInexakt()、setInExaktRepeating()など。問題は、電話がスリープ状態にあるときに、RTC_WAKE_UPを使用してもスリープ状態になっていないことです。さらに、私はAPI 19 +の問題の解決策を見いだせなかった。 1つの通知後に手動で次に設定する必要があるためです。それは私がサービスを設定した理由です。 –
"電話がスリープ状態になっても、RTC_WAKE_UPであっても目を覚ますことはありません" - 少なくともAndroid 6.0+(Dozeモード/アプリケーションスタンバイが干渉し始める)以前は他の開発者のために、バグはありません。 "さらに、私はapi 19 +の問題の解決策を見つけられませんでした。なぜなら、ある通知の後に手動で次の設定をする必要があるからです - もう一度、それは他の開発者のために働きます。 – CommonsWare