2013-04-08 56 views
0

如果強制停止,我試圖在接收短信時重新啓動我的應用程序。這是我的代碼。 它不重新啓動應用程序。我應該嘗試將接收器作爲另一個類編寫。 在清單:重新啓動力在收到短信時停止了應用程序

<uses-permission android:name="android.permission.SEND_SMS" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <receiver android:name=".MySMSbr"> 
    <intent-filter> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
    </receiver> 

My mainActivity onCreate() : 

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; 
BroadcastReceiver SMSbr; 

    public void onCreate(Bundle savedInstanceState) { 
    Toast.makeText(getApplicationContext(),"in OnCreate", Toast.LENGTH_LONG).show(); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  
    SMSbr = new BroadcastReceiver() 
     { 

      @Override 
      public void onReceive(Context context,Intent intent) 
      { 
       this.abortBroadcast(); 
       Toast.makeText(context, "in onReceive", Toast.LENGTH_LONG).show(); 
       toggleLogging(AppSettings.getServiceRunning(MainActivity.this), 
         AppSettings.getLoggingInterval(MainActivity.this)); 
       this.clearAbortBroadcast(); 
      }//end of onReceive method 

      };//end of BroadcastReceiver 

      IntentFilter SMSfilter = new IntentFilter(SMS_RECEIVED); 
      this.registerReceiver(SMSbr, SMSfilter); 
    } 
    in togglelogging the service is started 
    where is it going wrong. 
+0

據我所知,力停止的應用程序不能用'Intent'在Android 3.x和較新的 – 2013-04-08 11:38:14

+0

我該怎麼辦,然後 – Manasi 2013-04-08 11:39:19

+0

服務在另一個進程開始應在這種情況下仍然存活 – 2013-04-08 12:06:00

回答

0

您聲明在清單中包含BroadcastReceiver - 即,靜態接收機但事實上,你沒有這樣的一類和你創建你的活動動態接收機。

實際上你在做什麼是當你的活動開始時註冊你的接收器,但你想要另一種方式(一旦接收器接收到廣播,開始你的活動/應用程序)。
您需要創建一個名爲SMSbr extends BroadcastReceiver的類,您可以在其中執行邏輯。

這樣你就可以讓接收機始終註冊,當收到SMS廣播時它會喚醒你的應用程序。

相關問題