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?
然而,我一直无法让这个与警报管理器一起工作,我不确定如何将警报意图与广播接收器联系起来。有人能给我举一个动态注册警报管理器广播接收器的例子吗?或者解释一下我该怎么做?
2 个解决方案
#1
16
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.xml
file:
然后在你的清单上。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.)
因此,据我所知,你仍然需要在清单中声明接收方。我不确定是否可以将其设置为活动内部的私有实例。您可以在活动中声明onReceive并调用它(如果BroadcastReceiver有接口的话)。我不知道它是不是。
#2
1
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!
你也可以设置重复报警。希望它帮助!
#1
16
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.xml
file:
然后在你的清单上。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.)
因此,据我所知,你仍然需要在清单中声明接收方。我不确定是否可以将其设置为活动内部的私有实例。您可以在活动中声明onReceive并调用它(如果BroadcastReceiver有接口的话)。我不知道它是不是。
#2
1
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!
你也可以设置重复报警。希望它帮助!