我有一个问题,我希望有些东西可以指出它为什么会发生。我硬编码了我想要触发警报的日期,并且在第一次运行应用程序时,它只会及时触发该实例。但是,一旦警报被触发,每次应用程序打开时,警报会不断触发并显示通知。这只是一个小应用测试,将用作更大的应用程序的一部分,并希望有人可以帮助我
package com.example.alarmtest;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.DAY_OF_MONTH, 12);
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 26 );
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
}
package com.example.alarmtest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyAlarmService extends Service {
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
package com.example.alarmtest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.alarmtest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.alarmtest.NextActivity"
android:label="Next Screen">
</activity>
<service android:name="com.example.alarmtest.MyAlarmService"
android:enabled="true" />
<receiver android:name=".MyReceiver"/>
</application>
</manifest>
答案 0 :(得分:1)
我认为您重新打开应用的大部分时间都会调用MainActivity
onCreate()
方法。因此,您将多次设置警报。以后的警报将在过去设置(我假设您为当前时间设置了第一个警报)并将立即触发。
答案 1 :(得分:0)
好的,所以我有类似的情况,我将通过可能的修复,但首先让我们检查一下为什么会发生这种情况。
在我的情况下,当您打开应用程序时,它会设置当天的警报,例如,如果设置的警报在当前时间之前,它将在设置后立即触发。因为我在闹钟接收器中显示通知,大多数时候我打开应用程序,早上的闹钟弹出。这证实了上述情况。
要停止此操作,请检查仅为将来设置警报。或者将此检查放入警报接收器中,如果警报时间小于当前时间,则不要点火通知。
在设置闹钟之前,我将意图设置闹钟时间(以毫秒为单位):
long timestamp=1111882929; // your alarm timestamp
alarmIntent.putExtra("timestamp",timestamp);
//... setAlarm...
当您收到onRecieve
AlarmReceiver
的{{1>}(在您的情况下,其Service
类)时,请先显示检查,然后再显示以下通知:
if(intent.getLongExtra("timestamp",0)> System.currentTimeMillis())
{
showNotification(intent);
}
// else do nothing
此外,如果您设置具有相同待处理请求代码的警报,例如:
int CODE=112; // any code
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast( context, CODE, alarmIntent, 0 );
这将使用代码112
设置闹钟,再次使用此代码设置闹钟将再次覆盖它。