如何自动启动一个AlarmManager开始预定活动? [英] How to Autostart an AlarmManager to start a Scheduled Activity?

查看:208
本文介绍了如何自动启动一个AlarmManager开始预定活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本教程来自的android-ER
主要活动(AndroidScheduledActivity.java)开始AlarmManager触发广播接收器(MyScheduledReceiver.java)反复。在MyScheduledReceiver的的onReceive()方法,启动另一个活动(MyScheduledActivity.java)间接。这样的活动(MyScheduledActivity.java)将在预定的时间间隔来启动。
现在,我会用自动启动自动启动,但我无法写AutoStartNotifyReceiver。
请你能给我一个想法如何管理呢?
非常感谢!

主要活动,AndroidScheduledActivity.java:

 公共类AndroidScheduledActivity延伸活动{  / **当第一次创建活动调用。 * /
  @覆盖
  公共无效的onCreate(捆绑savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.main);
      按钮buttonStart =(按钮)findViewById(R.id.start);
      buttonStart.setOnClickListener(新Button.OnClickListener(){  @覆盖
  公共无效的onClick(查看为arg0){
    意图myIntent =新意图(getBaseContext()
      MyScheduledReceiver.class);    的PendingIntent的PendingIntent
     = PendingIntent.getBroadcast(getBaseContext(),
       0,myIntent,0);    alarmManager alarmManager
      =(AlarmManager)getSystemService(ALARM_SERVICE);
    台历挂历= Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis的());
    calendar.add(Calendar.SECOND,10);
    长间隔= 60 * 1000; //
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
      calendar.getTimeInMillis(),间隔的PendingIntent);
    完();
  }});
  }}

然后广播接收器,MyScheduledReceiver.java

 公共类MyScheduledReceiver扩展广播接收器{公共无效的onReceive(上下文的背景下,意图意图){ 意图scheduledIntent =新意图(背景下,MyScheduledActivity.class);
 scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(scheduledIntent);
}
}

和我的问题AutoStartNotifyReceiver:

 公共类AutoStartNotifyReceiver扩展广播接收器{ 私人最终字符串BOOT_COMPLETED_ACTION =android.intent.action.BOOT_COMPLETED; @覆盖
 公共无效的onReceive(上下文的背景下,意图意图){  如果(intent.getAction()。等于(BOOT_COMPLETED_ACTION)){????????????????????  } }
}


解决方案

AutoStartNotifyReceiver延伸广播接收器类是存在的,因为报警被清除设备重置时。所以,在的onReceive 这个类(你有问号),您需要使用相同的code(从头再来设置闹钟当然没有的你用来做什么的的onClick 法首次完成()) > AndroidScheduledActivity 。

然后,你需要把下面的条目在清单来让系统知道要启动你的 AutoStartNotifyReceiver 当系统启动:

 <接收机器人:名字=。AutoStartNotifyReceiver>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
        &所述; /意图滤光器>
< /接收器>

除了在清单权限

 <使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>

现在,这是所有假设你只有一个报警,仅设置它每次的一种方式。如果这不是这种情况比这变得更加复杂。但是,根据您提供的信息很少,我的解决办法应该做你想要什么。

此外,因为你是这里的新人只是一个善意的提醒:当有人提供充分回答一个问题,问这个问题(你)的人接受通过单击旁边的答案复选框答案。这是为了让应答者得到信贷。欢迎来到SO!

This tutorial come from android-er, The main activity(AndroidScheduledActivity.java) start a AlarmManager to trigger BroadcastReceiver(MyScheduledReceiver.java) repeatly. In the onReceive() method of MyScheduledReceiver, it start another activity(MyScheduledActivity.java) indirectly. Such that the activity(MyScheduledActivity.java) will be start in scheduled interval. Now I would use AutoStart to start automatically, but I was not able to write the AutoStartNotifyReceiver . please can you give me an idea how to manage it ? Thanks a LOT !

main activity, AndroidScheduledActivity.java :

public class AndroidScheduledActivity extends Activity {

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      Button buttonStart = (Button)findViewById(R.id.start);
      buttonStart.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
    Intent myIntent = new Intent(getBaseContext(),
      MyScheduledReceiver.class);

    PendingIntent pendingIntent
     = PendingIntent.getBroadcast(getBaseContext(),
       0, myIntent, 0);

    AlarmManager alarmManager
      = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    long interval = 60 * 1000; //
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
      calendar.getTimeInMillis(), interval, pendingIntent);
    finish();
  }});
  }

}

Then BroadcastReceiver, MyScheduledReceiver.java

public class MyScheduledReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

 Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
 scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(scheduledIntent);
}
}

and my problem AutoStartNotifyReceiver :

public class AutoStartNotifyReceiver extends BroadcastReceiver {

 private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";

 @Override
 public void onReceive(Context context, Intent intent) {

  if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){

????????????????????

  }

 }
}

解决方案

Your AutoStartNotifyReceiver extends BroadcastReceiver class is there because the alarms get cleared when the device resets. So, in the onReceive of this class (where you have the question marks) you need to set the alarm all over again with the same code (without, of course the finish())that you used to do it the first time in the onClick method of AndroidScheduledActivity.

Then, you need to put the following entry in your Manifest to let the system know to launch your AutoStartNotifyReceiver when the system boots up:

<receiver android:name=".AutoStartNotifyReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
</receiver>

As well as a permission in the Manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Now, this is all assuming you only have one alarm and only set it one way every time. If that is not the case than this gets a bit more complicated. But based on the little info you provided, my solution should do what you want.

Also, since you are a newcomer here just a kindly reminder: when someone provides an adequate answer to a question, the person asking the question (you) accepts the answer by clicking the checkbox next to the answer. This is so the person answering gets credit. Welcome to SO!

这篇关于如何自动启动一个AlarmManager开始预定活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆