2013-07-17 113 views
0

嗨,目前我正在執行安卓通知功能使用報警管理器和服務,但是當我在Android設備上啓動應用程序時,它顯示應用程序意外停止當我從應用程序中刪除應用程序按住主頁按鈕即可顯示「最近的應用」列表。當應用程序未從「最近的應用程序」列表中刪除時,它工作正常。安卓服務意外停止

任何人都可以建議我在這裏做錯了什麼? 感謝您的回覆。

我有一個活動讓用戶決定他們是否想要收到通知。然後該值傳遞給Trade.java

Trade.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); 
     } 

MyService.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); 
    } 
} 

的Manifest.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> 
+1

請張貼您的'日誌貓' –

+0

您能否更好地解釋您的問題?你如何刪除一個持有主頁按鈕的應用程序?如何從最近的應用程序中刪除應用程序?你是在殺死應用程序進程還是恢復它? –

+0

@chintankhetiya如何在Android設備上運行時看到日誌貓?謝謝 – jazebel

回答

1

嘗試增加START_STICKY作爲返回價值在您的服務的onstartCommand()方法