Wie erstellen Sie persistente Alarme auch nach dem Neustart

Derzeit arbeite ich an der app, dass Werke wie "To Do Task List". Ich habe erfolgreich das NotificationService und SchedularService in meiner Anwendung. Auch ich bekomme die Benachrichtigungen(Notifications) bei der Einstellung der Zeit für die Aufgaben.
Hier sind meine Fragen wie folgt:

  1. Mit diesem code wird meine Alarme werden nach einem Neustart gelöscht werden ? Wenn ja, wie dies zu überwinden.
  2. Ich hielt mich an der Priorität der Aufgaben. Aber ich möchte den Mechanismus so dass, wenn der Benutzer wählt mit der Priorität "Hoch", dann sollte er Benachrichtigungen erhalten dreimal, sagen wir, vor 30 Minuten, bevor Sie 15 Minuten und die Zeit eingestellt. Wie dies zu erreichen ?
  3. Ich einstellen will Handy vibriert bei Benachrichtigungen ausgelöst werden. Wie dies zu erreichen ?
  4. Und ich möchte wissen, was getan werden kann, für die veralteten Methoden und der Konstruktor in NotifyService.java. Thesse veraltet sind in der API-level 11: Notification notification = new Notification(icon, text, time); und notification.setLatestEventInfo(this, title, text, contentIntent);. Auf developer.android.com Sie haben vorgeschlagen, um zu Verwenden Notification.Builder statt. So wie man meine app kompatibel mit allen API-levels.

Hier ist mein code-snippet für die Planung alarm:

...
scheduleClient.setAlarmForNotification(c, tmp_task_id);
...

Hier ist die Klasse ScheduleClient.java:

public class ScheduleClient {

    private ScheduleService mBoundService;
    private Context mContext;
    private boolean mIsBound;

    public ScheduleClient(Context context)
    {
        mContext = context;
    }

    public void doBindService()
    {   
        mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {

            mBoundService = ((ScheduleService.ServiceBinder) service).getService();
        }

        public void onServiceDisconnected(ComponentName className) {

            mBoundService = null;
        }
    };

    public void setAlarmForNotification(Calendar c, int tmp_task_id){

        mBoundService.setAlarm(c, tmp_task_id);
    }

    public void doUnbindService() {
        if (mIsBound)
        {           
            mContext.unbindService(mConnection);
            mIsBound = false;
        }
    }
}

Hier ist die ScheduleService.java:

public class ScheduleService extends Service {

    int task_id;

    public class ServiceBinder extends Binder {

        ScheduleService getService() {

            return ScheduleService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {

        return mBinder;
    }

    private final IBinder mBinder = new ServiceBinder();

    public void setAlarm(Calendar c, int tmp_task_id) {

        new AlarmTask(this, c, tmp_task_id).run();
    }
}

Hier ist die AlarmTask.java:

public class AlarmTask implements Runnable{

    private final Calendar date;
    private final AlarmManager am;
    private final Context context;
    int task_id;

    public AlarmTask(Context context, Calendar date, int tmp_task_id) {
        this.context = context;
        this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        this.date = date;

        task_id = tmp_task_id;
    }

    @Override
    public void run() {

        Intent intent = new Intent(context, NotifyService.class);
        intent.putExtra(NotifyService.INTENT_NOTIFY, true);
        intent.putExtra("task_id", task_id);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

        am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
    }
}

Hier ist die NotifyService.java:

public class NotifyService extends Service {

    public class ServiceBinder extends Binder
    {   
        NotifyService getService()
        {
            return NotifyService.this;
        }
    }

    int task_id;
    private static final int NOTIFICATION = 123;
    public static final String INTENT_NOTIFY = "com.todotaskmanager.service.INTENT_NOTIFY";
    private NotificationManager mNM;
    SQLiteDatabase database;

    @Override
    public void onCreate() {

        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String tmp_task_brief = null;
        task_id = intent.getIntExtra("task_id", 0);

        loadDatabase();
        Cursor cursor = database.query("task_info", new String[]{"task_brief"}, "task_id=?", new String[]{task_id+""}, null, null, null);
        while(cursor.moveToNext())
        {
            tmp_task_brief = cursor.getString(0);
        }
        cursor.close();

        if(intent.getBooleanExtra(INTENT_NOTIFY, false))
            showNotification(tmp_task_brief);

        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {

        return mBinder;
    }

    private final IBinder mBinder = new ServiceBinder();

    private void showNotification(String tmp_task_brief) {

        CharSequence title = "To Do Task Notification!!";
        int icon = R.drawable.e7ca62cff1c58b6709941e51825e738f;
        CharSequence text = tmp_task_brief;     
        long time = System.currentTimeMillis();

        Notification notification = new Notification(icon, text, time);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TaskDetails.class), 0);

        notification.setLatestEventInfo(this, title, text, contentIntent);

        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        mNM.notify(NOTIFICATION, notification);

        stopSelf();
    }

    void loadDatabase()
    {
        database = openOrCreateDatabase("ToDoDatabase.db",
                SQLiteDatabase.OPEN_READWRITE, null);
    }
}
InformationsquelleAutor Chintan Soni | 2013-05-14
Schreibe einen Kommentar