0

今後の映画を一覧表示するアプリを作成しています。ユーザーは、リリースが近づいたときに通知したい映画のリマインダーを設定します (DatePicker を使用して、通知がポップアップする日付を選択します)。つまり、各映画が通知を受け取ることができると推測しました。ユーザーがそのようにリマインダーを設定するときに、映画の名前と ID を SharedPreference に入れる必要があります。

public void setAlarm(View view){ 

     Intent alertIntent = new Intent(this, AlertReceiver.class);

     SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
     SharedPreferences.Editor editor = settings.edit();
     editor.putString("name", name);
     editor.putInt("id", mainId);
     editor.commit();

     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // set() schedules an alarm to trigger
    // FLAG_UPDATE_CURRENT : Update the Intent if active
    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
            PendingIntent.getBroadcast(this, 1, alertIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT));
}

次に、OnRecieve() メソッドで SharedPreference を取得し、名前と ID を使用してメッセージを作成します。

public void onReceive(Context context, Intent intent) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    String name = settings.getString("name", "");
    int id = settings.getInt("id", 0);
    createNotification(context, "" ,  name + " Coming soon" , name, id);
}


public void createNotification(Context context, String msg, String msgText, String msgAlert, int id){

    // Define an Intent and an action to perform with it by another application
    PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);

    // Builds a notification
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setContentTitle(msg)
            .setContentText(msgText)
            .setTicker(msgAlert)
            .setSmallIcon(R.mipmap.ic_launcher);

    //the intent when the notification is clicked on
    mBuilder.setContentIntent(notificIntent); //goes to MainActivity

    //how the user will be notified
    mBuilder.setDefaults(NotificationCompat.DEFAULT_LIGHTS);

    //stop notification when it's clicked on
    mBuilder.setAutoCancel(true);

    //now to notify the user with NotificationManager
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(id, mBuilder.build());
}

OnReceive はうまく機能しますが、SharedPreference が古いものを新しいもので上書きするように、一度に 1 つのリマインダーしか作成できなかったようです。setAlarm() 先のムービーごとに新しい SharedPreference を宣言する必要がありますか? その後、BroadcastReceiver の OnReceive メソッドは SharedPreference から値を取得しますか?

4

2 に答える 2

0

この場合、SharedPreference は必要ありません。直接データをインテントに入れ、そのデータを onReceive メソッドのインテントから取得できます。

複数のアラーム (繰り返しまたは単一) を設定する場合は、異なる requestCode で PendingIntents を作成するだけです。requestCode が同じ場合、新しいアラームが古いアラームを上書きします。これには System.currentTimeMillis(); を使用できます。

public void setAlarm(View view){ 

     Intent alertIntent = new Intent(this, AlertReceiver.class);

     alertIntent.putExtra("name", name);
     alertIntent.putExtra("id", mainId);


     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // set() schedules an alarm to trigger
    // FLAG_UPDATE_CURRENT : Update the Intent if active
    alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
            PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), alertIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT));
}

onReceive メソッドはインテントからデータを取得します

public void onReceive(Context context, Intent intent) {


if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
        //reset your alarm here save your alarms in database or shared    //preferences so that you can reset here
    }else{
        String name = intent.getStringExtra("name", "");
        int id = intent.getIntExtra("id", 0);
        createNotification(context, "" ,  name + " Coming soon" , name, id);
}
    }
于 2015-10-16T04:49:00.990 に答える