带有以代码而非清单注册的广播接收器的 Android 警报管理器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3447594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:14:26  来源:igfitidea点击:

Android Alarm Manager with broadcast receiver registered in code rather than manifest

androidbroadcastreceiveralarmmanager

提问by Shane

I want to use an alarm to run some code at a certain time. I have successfully implemented an alarm with the broadcast receiver registered in the manifest but the way i understand it, this method uses a separate class for the broadcast receiver.

我想使用警报在特定时间运行一些代码。我已经成功地使用在清单中注册的广播接收器实现了警报,但按照我的理解,此方法为广播接收器使用了一个单独的类。

I can use this method to start another activity but I cant use it to run a method in my main activity?

我可以使用此方法启动另一个活动,但我不能使用它在我的主要活动中运行方法?

(how can I notify a running activity from a broadcast receiver?)

如何从广播接收器通知正在运行的活动?

So I have been trying to register my broadcast receiver in my main activity as explained in the answer above.

所以我一直在尝试在我的主要活动中注册我的广播接收器,如上面的答案所述。

private BroadcastReceiver receiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "hello", Toast.LENGTH_SHORT).show();
        uploadDB();         
    }
};    

public void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter();
    filter.addAction(null);

    this.registerReceiver(this.receiver, filter);
}

public void onPause() {
    super.onPause();

    this.unregisterReceiver(this.receiver);
}

However I have been unable to get this to work with alarm manager, I am unsure as to how i should link the alarm intent to the broadcast receiver. Could anyone point me to an example of registering an alarm manager broadcast receiver dynamically in the activity? Or explain how i would do this?

但是我一直无法让它与警报管理器一起工作,我不确定我应该如何将警报意图链接到广播接收器。谁能指出我在活动中动态注册警报管理器广播接收器的示例?或解释我将如何做到这一点?

回答by user123321

How about this?

这个怎么样?

Intent startIntent = new Intent("WhatEverYouWant");
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, startPIntent);

And then in your Manifest.xmlfile:

然后在您的Manifest.xml文件中:

<receiver android:name="com.package.YourOnReceiver">
   <intent-filter>
       <action android:name="WhatEverYouWant" />
   </intent-filter>
</receiver>

So as far as I know you still have to declare the receiver in the Manifest. I'm not sure if you can set it to a private instance inside of your activity. You could declare an onReceive inside of your activity and call that (if the BroadcastReceiver has an interface. I don't know if it does.)

所以据我所知,你仍然需要在 Manifest 中声明接收者。我不确定您是否可以将其设置为您活动中的私有实例。您可以在您的活动中声明一个 onReceive 并调用它(如果 BroadcastReceiver 有一个接口。我不知道它是否有。)

回答by Dhrupal

Start a alarm intent from where you want to start alarm. write below code from where you want to start to listen the alarm

从您要启动警报的位置启动警报意图。从您要开始收听警报的位置编写以下代码

Intent myIntent = new Intent(getBaseContext(), **AlarmReceiver**.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.MINUTE, shpref.getInt("timeoutint", 30));
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

And in broadcast receiver write the code you want to receive. And in menifest write below

并在广播接收器中编写您想要接收的代码。在menifest中写在下面

<receiver android:name=".AlarmReceiver" android:process=":remote"/>

You can also put repetitive alarm also. Hope it help!

您也可以设置重复警报。希望有帮助!