嗨,目前我正在使用警报管理器和服务在 android 上实现通知功能,但是当我在 android 设备上启动应用程序时,它会显示当我从“最近的应用程序”列表中删除应用程序时应用程序意外停止的错误消息按住主页按钮。当应用程序未从“最近的应用程序”列表中删除时,它可以正常工作。
谁能建议我在这里做错了什么?感谢您的回复。
我有一个活动让用户确定他们是否要接收通知。然后将该值传递给 Trade.java
贸易.java
//the part where alarm manager is trigger
if(spApp_checkbox.getBoolean("checkbox", false) == true){
if(spApp_checkbox.getInt("time", 0) <= 10){
setTime = 60*1000;
}
else{
setTime = spApp_checkbox.getInt("time", 0)*1000;
}
Calendar calender = Calendar.getInstance();
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
intent.setClass(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), setTime, pendingIntent);
this.startService(intent);
}
else{
AlarmManager alarmManagerstop = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intentstop = new Intent(this, MyService.class);
PendingIntent senderstop = PendingIntent.getService(getApplicationContext(), 0, intentstop, 0);
// Cancel alarms
try {
alarmManagerstop.cancel(senderstop);
} catch (Exception e) {
Log.e("TAG", "AlarmManager update was not canceled. " + e.toString());
}
this.stopService(intentstop);
}
我的服务.java
public class MyService extends Service {
private static String SOAP_ACTION1 = "http://pagecode/getTradeControlList_ByParameters";
private static String NAMESPACE = "http://pagecode";
private static String METHOD_NAME1 = "getTradeControlList_ByParameters";
private static String URL = "";
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Service Destroy", 1).show();
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
// action to determine whether notification will be trigger
Vector<SoapObject> obj1_new = showTrade(callby, tradeCrtl, days);
int[] id_new = new int[obj1_new.size()];
if(obj1_new != null){
for (int i = 0; i < obj1_new.size(); i++) {
id_new[i] = Integer.valueOf(words[0]);
if(!hm.containsKey(id_new[i])){
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
PendingIntent in = PendingIntent.getActivity(getApplicationContext(), 0, intent_tr, 0);
System.out.println("list changes");
mBuilder.setContentTitle("Application")
.setContentText("New trade available")
.setProgress(0, 0, false)
.setSmallIcon(R.drawable.ic_logo)
.setDeleteIntent(in)
.setAutoCancel(true);
mBuilder.setContentIntent(in);
mNotificationManager.notify(0, mBuilder.build());
hm = new HashMap();
}
}
for (int j = 0; j < obj1_new.size(); j++) {
hm.put(id_new[j], id_new[j]);
}
}
return super.onStartCommand(intent, flags, startId);
}
}
清单.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android.app.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.android.app.AppSettings"></activity>
<activity android:name="com.android.app.Trade"
android:windowSoftInputMode="stateHidden|adjustPan"></activity>
<service
android:name="com.android.app.MyService"
android:enabled="true"
android:exported="true" >
</service>
</application>
</manifest>