【问题标题】:How to set Scheduled notification in android如何在android中设置计划通知
【发布时间】:2013-06-17 12:18:04
【问题描述】:

我希望我的应用在 10 秒后发出通知,即使应用未运行也是如此。我已使用AlarmManager,但无法在所需时间收到通知。 那时我可以祝酒,但不能收到通知。任何帮助将不胜感激。

这是我的 AlarmManager 类:

public class AlarmManagerActivity extends Activity {

private AlarmManagerBroadcastReceiver alarm;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alarm_manager);
    alarm = new AlarmManagerBroadcastReceiver();
    onetimeTimer();


}

@Override
protected void onStart() {
    super.onStart();
}


public void notifyN(){

    Intent intent = new Intent(this, NotificationReceiverActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

    // Build notification
    // Actions are just fake
    Notification noti = new Notification.Builder(this)
            .setContentTitle("This is a sample notification")
            .setContentText("Subject")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .addAction(R.drawable.ic_action_search, "Call", pIntent)
            .addAction(R.drawable.ic_action_search, "More", pIntent)
            .addAction(R.drawable.ic_action_search, "And more", pIntent).build();


    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Hide the notification after its selected
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, noti);
}


public void onetimeTimer(){
    Context context = this.getApplicationContext();
    if(alarm != null){
        alarm.setOnetimeTimer(context);
        notifyN();

    }else{
        Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
    }
}

}

这是 AlarmManagerBroadcastReceiver:

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

final public static String ONE_TIME = "onetime";

@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 update the widget/remote views.
    Bundle extras = intent.getExtras();
    StringBuilder msgStr = new StringBuilder();

    if (extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)) {
        msgStr.append("One time Timer : ");
    }
    Format formatter = new SimpleDateFormat("hh:mm:ss a");
    msgStr.append("abcs stop the instance" + formatter.format(new Date()));

    Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();

    // Release the lock
    wl.release();

}

public void setOnetimeTimer(Context context) {
    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.TRUE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pi);

}
}

【问题讨论】:

    标签: android notifications alarmmanager alarm


    【解决方案1】:

    创建一个类(将其称为ScheduledService)并让它扩展IntentService。在本课程中,您将在闹钟响起时做您想做的事情。

    public class ScheduledService extends IntentService {
    
    public ScheduledService() {
        super("My service");
    }
    
    @Override
    protected void onHandleIntent(Intent intent) {
        //Do something, fire a notification or whatever you want to do here
        Log.d("debug", "Ring Ring!");
    
    }
    }
    

    然后在您的活动中使用以下命令启动警报:

    AlarmManager mgr = (AlarmManager) YourActivity.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(YourActivity, ScheduledService.class);
    PendingIntent pi = PendingIntent.getService(YourActivity, 0, i, 0);
    mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + PERIOD, pi);
    

    其中PERIOD 是您希望闹钟响起的时间(以毫秒为单位)。

    要取消闹钟使用:

    if (mgr != null)
            mgr.cancel(pi);
    

    最后,您需要将 ScheduledService 类注册为服务。 在你的清单中添加这个到你的应用程序:

    <application
        ... />
        ...
        <service android:name=".ScheduledService" >
        </service>
    
    </application>
    

    这样,Android 操作系统会在适当的时候发出警报。即使另一个应用程序正在运行或您的应用程序进程已终止。

    【讨论】:

    • 当您在 10 小时内设置警报通知并且用户必须重新启动手机时,这不起作用,因为计划任务仅在进程正在运行的线程时有效,当恢复时不知道他有一个待处理的任务.在这种情况下,您可以使用 BroadcastReceiver 和 android.intent.action.BOOT_COMPLETED:stackoverflow.com/questions/2784441/…
    猜你喜欢
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多