这是我的第一篇文章,我是Android App开发的新手,所以请耐心等待。
我已经写了(并且每天使用)一个非常好的提醒应用程序,因为所有其他都太复杂或太简单。它也在游戏商店,虽然不容易找到,但那是另一个故事。
虽然我对整个应用程序感到满意,但显示通知或发出警报的警报并不总是发生。如果警报很快就会发生,但是从现在起2个小时以后它会经常被错过。使用" adb shell dumpsys alarm"它看起来好像被丢弃了?
我已经搜索了好几天,并尝试了很多东西,也来自这个论坛,但没有运气,所以我想我会发布一些代码,看看有没有人可以告诉我,我错了,因为它应该工作到目前为止我可以看到。
所以这里。
我使用警报管理器对未决意图进行排队,以调用我的广播接收器,该接收器启动一项服务,该服务启动一项活动,该活动会创建通知并可选择发出警报。
我正在跳过服务位但谷歌搜索后似乎应该使用服务。如上所述,这适用于很快发生的警报,但不会在几小时或一天或更长时间之后发生。可能是由于打盹模式还是我的应用被Android停止了?
我真的很感激一些帮助,因为我要把头发拉出来!
提前致谢。
这是我的清单。我认为问题在哪里?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wingwares.reminders">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<supports-screens
android:anyDensity="true"
android:normalScreens="true" />
<application
android:icon="@drawable/reminder2"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<receiver android:name=".WidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<service
android:name=".WidgetService"
android:exported="false"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<activity
android:name=".AppPreferences" />
<activity
android:name=".ListActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".RemActivity"
android:label="@string/add_reminder"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" />
<service
android:name=".AlarmService"
android:enabled="true"
android:exported="true" >
</service>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="SET_ALERT" />
</intent-filter>
</receiver>
<activity
android:name=".AlarmDialog"
android:theme="@style/AlarmDialog" />
</application>
</manifest>
来自ListActivity的我的闹钟管理器
public void setAlert(Context context,boolean turnOn){
intent = new Intent(context, AlarmReceiver.class);
intent.putExtra("name", fullName());
intent.putExtra("notes", notes);
intent.putExtra("setalarm", setalarm);
intent.putExtra("millis", time.getMilliTime());
intent.putExtra("remId", remId);
pendingIntent = PendingIntent.getBroadcast(context, 8192 + remId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
if (turnOn) {
alertTime = time.getMilliTime() - (warning * 60000);
interval = getInterval();
//set repeating alarmManager just once
if (freq.equals(EVERY) && type.equals(FIXED) && date.equals(getOriginal()) && interval > 0) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alertTime, interval, pendingIntent);
} else {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
}
}
} else {
setalarm = false;
}
}
我的广播接收器
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent(context, AlarmService.class);
//pass the extras on and show the dialog
newIntent.putExtras(intent);
startWakefulService(context, newIntent);
}
}
我的服务
public class AlarmService extends IntentService {
public AlarmService (){
super("AlarmService");
}
@Override
protected void onHandleIntent(Intent intent) {
Intent newIntent = new Intent(this, AlarmDialog.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//pass the extras on and show the dialog
newIntent.putExtras(intent);
startActivity(newIntent);
AlarmReceiver.completeWakefulIntent(newIntent);
}
}
我的活动(通知部分)
public class AlarmDialog extends Activity
implements DialogInterface.OnCancelListener, DialogInterface.OnClickListener {
Bundle extras;
int remId;
String name, notes;
Boolean setalarm;
long alarmTime;
MediaPlayer player;
PendingIntent pendingIntent;
SharedPreferences mPrefs;
NotificationManager manager;
Notification notification;
static final int SNOOZE_BUTTON = -2;
static final int DISMISS_BUTTON = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create notification and alert box if alarmManager selected
mPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
extras = getIntent().getExtras();
alarmTime = extras.getLong("millis");
remId = extras.getInt("remId");
name = extras.getString("name");
notes = extras.getString("notes");
setalarm = extras.getBoolean("setalarm");
//notification first
notification = new RemNotification(this, name).get();
if (notification != null) {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(remId);
manager.notify(remId, notification);
}
}