现有使用广播接收器的应用程序存在后,是否有办法使重复警报正常工作? [英] Is there a way to keep a repeating alarm working after existing the app that uses a broadcast receiver?

查看:74
本文介绍了现有使用广播接收器的应用程序存在后,是否有办法使重复警报正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手。我正在尝试创建一个应用程序,该应用程序使用 BroadcastReceiver 在由重复警报触发的主要活动上执行功能。我读到必须动态注册 broadcastReceiver ,这是为了能够在主活动上执行该功能。我面临的问题是,一旦退出应用程序,警报就会停止工作。我读到这是设计使然-有没有办法解决这个问题,还是必须使用服务

I am new on Android. I am trying to create an application that uses BroadcastReceiver to execute a function on the main activity triggered by a repeating alarm. I read that I had to dynamically register the broadcastReceiver which I did - this is to be able to execute the function on the main activity. The problem I am faced with is that as soon as the app is exited, the alarm stops working. I read that this is by design - is there a way to overcome this or do I have to use a service? Thanks in advance.

示例代码:

public class AlarmReceiver extends BroadcastReceiver {  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        Toast.makeText(context, "from AlarmReceiver", Toast.LENGTH_SHORT).show();  
    }
}

public class MainActivity extends AppCompatActivity {  
    private PendingIntent pendingIntent;  
    private AlarmManager manager;  
    private AlarmReceiver myReceiver = null;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        myReceiver = new AlarmReceiver();  
        IntentFilter myIntentFilter = new IntentFilter("ANY_ACTION");  
        registerReceiver(myReceiver,  myIntentFilter);  
        Intent myIntent = new Intent();  
        myIntent.setAction("ANY_ACTION");  
        pendingIntent = PendingIntent.getBroadcast(this, 0,myIntent,0);  
    }  
      public void startAlarm(View view) {  
        manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);  
        int interval = 1500;  
        manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),   interval, pendingIntent);  
        Toast.makeText(this, "Alarm Set", Toast.LENGTH_LONG).show();  
    }     
}


推荐答案

I认为,当您的应用未运行时,警报停止工作的原因是您正在使用 registerReceiver 在本地注册 AlarmReceiver 。如果您想注册AlarmReceiver以便即使您的应用未运行也能正常工作,则需要在 AndroidManifest.xml 中进行注册。

I think, the reason why your alarm stops working when your app isn't running is that you're registering your AlarmReceiver locally with registerReceiver. If you want to register your AlarmReceiver so that it keeps working even when your app isn't running, you need to register it inside AndroidManifest.xml.

首先,将接收者添加到 AndroidManifest.xml 中,如下所示:

Firstly, add your reciever into AndroidManifest.xml like this:

  <application>    
       //...
       <receiver android:name=".AlarmReceiver">
        </receiver>
       //...
  </application>

并像这样设置闹钟(请记住:不要将闹钟间隔设置得太短,至少要设置1分钟;您在ur代码中将ur间隔设置为1.5秒-可能不起作用):

And set your alarm like this (Remember: Don't set alarm interval too short, set at least 1 minute; you set ur interval 1,5 second in ur code - it may not work):

    int interval = 60 * 1000;
    Intent intent=new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

当警报在onRecieve内部触发时,只需执行您想做的事情即可:

And just do what you want to do when alarm triggers inside onRecieve:

public class AlarmReceiver extends BroadcastReceiver {
     @Override
     public void onReceive(Context context, Intent intent) {

    AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    SharedPreferences sharedPreferences = context.getSharedPreferences("Settings", 0);
    int iDefValue=0; 
    int iDayAlarmVal= sharedPreferences.getInt("something", iDefValue); 
    // and so on...
                 }
         }

这篇关于现有使用广播接收器的应用程序存在后,是否有办法使重复警报正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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