我有两个问题的报警管理器。一个是我想每10秒重复一次警报,另一个是我的广播接收器在10秒后调用。
当我使用Activity而不是Broadcast Receiver时,它正在工作,但Broadcast Receiver不工作。时间是不合适的,有的时间是1分钟,有的时间是50秒。但我需要每10秒一次。
我的代码是:
buttonStart.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
long tenMin = 1000/5 ;//here is 30 seconds
Intent intent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
0, intent, 0);
AlarmManager am =
(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), tenMin, pendingIntent);
}});
Boradcastereceiver公司。Java语言
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
Manifest file:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyAlarmService" />
<receiver
android:name=".MyBroadcastReceiver"
android:process=":remote"></receiver>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>