我在特定时间重复设计报警管理器。但是当我在点击方法中取消它后,秒(时间间隔)服务自动启动。
我希望按钮点击激活服务并在间隔中重复。并且在重新打电话之后也想开始服务。
任何想法?
这是我的清单代码:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarm"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver" android:enabled="true"/>
<receiver android:name=".BootCompleteReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".AlarmService" />
</application>
</manifest>
My AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "BANANEALARM";
private static final int NOTIFICATION_ID = 1;
Intent intent;
PendingIntent pendingIntent;
NotificationManager notificationManager;
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "starttt", Toast.LENGTH_LONG).show();
// if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Log.d(TAG, "in onreceive");
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
// }
}
}
我的BootCompleteReceiver
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Toast.makeText(context, "Boot", Toast.LENGTH_LONG).show();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
this.scheduleAlarms(context, calendar);
}
}
private void scheduleAlarms(Context ctxt, Calendar c) {
AlarmManager alarManager = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctxt, AlarmService.class);
PendingIntent pi = PendingIntent.getService(ctxt, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
alarManager.setRepeating(AlarmManager.RTC,c.getTimeInMillis(),getInterval(), pi);
}
private int getInterval(){
int days = 0;
int hours = 0;
int minutes = 1;
int seconds = 60;
int milliseconds = 1000;
int repeatMS = minutes* seconds * milliseconds;
return repeatMS;
}
}
我的MainActivity:
public class MainActivity extends Activity {
private static final String TAG = "BANANEALARM";
public AlarmManager alarmManager;
Intent alarmIntent;
PendingIntent pendingIntent;
NotificationManager notificationManager;
Button btn,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setAlarm();
}
});
btn2=(Button)findViewById(R.id.stop_s);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopservice();
}
});
}
public void stopservice()
{
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(MainActivity.this , "Cancel!", Toast.LENGTH_LONG).show();
}
public void triggerAlarm(View v){
setAlarm();
}
public void setAlarm(){
Intent intent = new Intent(this, AlarmReceiver.class);
intent.setAction("com.example.alarm.ACTION");
PendingIntent pendingIntent = PendingIntent
.getBroadcast(MainActivity.this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
AlarmManager alarm = (AlarmManager) MainActivity.this
.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pendingIntent);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
getInterval(), pendingIntent);
Log.d(TAG,"alarmStartTime: " );
}
private int getInterval(){
int days = 0;
int hours = 0;
int minutes = 1;
int seconds = 60;
int milliseconds = 1000;
int repeatMS = minutes* seconds * milliseconds;
return repeatMS;
}
@Override
protected void onStart(){
super.onStart();
cancelNotifications();
setAlarm();
}
public void cancelNotifications(){
notificationManager.cancelAll();
}
}
答案 0 :(得分:0)
在stopservice
方法代码alarmManager.cancel(pendingIntent)
中,pendingIntent
始终为空。
尝试替换
PendingIntent pendingIntent = PendingIntent
到
pendingIntent = PendingIntent
在您的setAlarm
方法中。
希望它有所帮助。