1

私が見ているのは、ローカル通知の実装です。アイデアは、日付が電話の SQL ファイルに保存され、現在の日付が SQL ファイルの日付の前の日付に達すると、ユーザーにメッセージで通知することです。

私はこれについて多くの調査を行い、アラームとサービスに関連するものを見つけました. 私は今、本当に混乱しており、どのルートを取るべきかわかりません. 誰か助けてくれませんか?

ありがとう

4

1 に答える 1

2

やりたいことは、通知をブロードキャストする AlarmManager を使用することです。データベースに日付を設定して管理する必要はありません。AlarmManager に日付を設定するだけです。このためのクラスを作成しました。このクラスには、アラームが鳴る時間を設定する機能と、ブロードキャストを受信して​​から通知を作成する機能があります。

public class MyAlarmManager extends BroadcastReceiver {
private Context mContext;
private AlarmManager mAlarmManager;

public MyAlarmManager() {}

public MyAlarmManager(Context context) {
    mContext = context;
    mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}

 @Override
 public void onReceive(Context context, Intent intent) {
     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
     //Acquire the lock
     wl.acquire();

     //You can do the processing here.      
     buildNotification(context);

     //Release the lock
     wl.release();
 }

public void setReminder(long time) {
    Intent intent = new Intent(mContext, MyAlarmManager.class);
    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, intent, 0);
    mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, pi); //time is in milliseconds
}

public void buildNotification(Context context) {
    long[] vibrate = { 0, 100, 200, 300 };

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setVibrate(vibrate)
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, YourLaunchActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ConferencesActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    int randomId = 1000303;
    mNotificationManager.notify(randomId, mBuilder.build());
}

}

次に、必ずこれをマニフェストに追加し、アプリのパッケージ名が何であれ使用してください。

<receiver android:name="com.example.myapp.MyAlarmManager"></receiver>

それが役立つことを願っています!

于 2013-05-16T22:31:20.090 に答える