2012-08-14 111 views
2

這是我的第一個Android程序。我有一項活動可以讀取信息outloud,一個接收器,用於偵聽傳入的SMS消息和一項服務,以使應用程序在後臺工作。Android:活動不會從後臺執行onNewIntent()(從服務調用)

我的主要活動有一個切換按鈕。啓用時,會啓動創建通知的服務,以防止應用程序被殺害。用戶也可以點擊通知即可返回主要活動:

package com.rockmanx77777.SMSbyVoice; 
import android.app.Notification; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Intent; 
import android.graphics.BitmapFactory; 
import android.os.Binder; 
import android.os.IBinder; 
import android.support.v4.app.NotificationCompat; 
import android.util.Log; 

public class SMSService extends Service{ 

    @Override 
    public void onCreate() { 
     createNotification(); 
     super.onCreate(); 
    } 

    public class MyBinder extends Binder { 
     SMSService getService() { 
      return SMSService.this; 
     } 
    } 

    private final IBinder binder = new MyBinder(); 

    @Override 
    public IBinder onBind(Intent intent) { 
     return binder; 
    } 

    private void createNotification(){ 

     final int NOTIFICATION_ID = 77777; 
     Intent intent = new Intent(this, SMSByVoice.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.putExtra("senderNumber", ""); 
     intent.setAction("com.rockmanx77777.SMSByVoice.PROCESS"); 
     PendingIntent pi = PendingIntent.getActivity(this, 1, intent, 0); 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

     builder.setContentTitle("Sample Title"); 
     builder.setContentText("Sample Text"); 
     builder.setSmallIcon(R.drawable.ic_launcher); 
     builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); 
     builder.setContentIntent(pi); 

     Notification notification = builder.getNotification(); 
     notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; 

     startForeground(NOTIFICATION_ID, notification); 
    } 
} 

當有來電短信服務的的onReceive正確調用。一切都被執行,包括context.startActivity(localSMS);正是在這裏,我遇到的問題:

package com.rockmanx77777.SMSbyVoice; 
import java.util.Set; 
import android.annotation.TargetApi; 
import android.app.ActivityManager; 
import android.app.IntentService; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.os.Bundle; 
import android.support.v4.content.LocalBroadcastManager; 
import android.telephony.SmsMessage; 
import android.util.Log; 

public class SMSReceiver extends BroadcastReceiver{ 

    public SMSReceiver(){ 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d("MSG", "Receiver's onReceived Called"); 
     if(intent.getAction() == "android.provider.Telephony.SMS_RECEIVED"){ 
      //Get the SMS message passed in 
      Bundle bundle = intent.getExtras();   
      SmsMessage[] msgs = null; 
      String senderNumber = ""; 
      String body = ""; 
      if (bundle != null){ 
       //Retrieve the SMS message received 
       Object[] pdus = (Object[]) bundle.get("pdus"); 
       msgs = new SmsMessage[pdus.length];    
       for (int i=0; i<msgs.length; i++){ 
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
        body = msgs[i].getMessageBody().toString();  
       } 
       senderNumber = msgs[0].getOriginatingAddress(); 

       Intent localSMS = new Intent(context, SMSByVoice.class); 
       localSMS.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       localSMS.setAction("rockmanx77777.SMSByVoice.RETURN"); 
       localSMS.putExtra("senderNumber", senderNumber); 
       localSMS.putExtra("body", body); 
       Log.d("MSG", "Starting activity from Receiver"); 
       context.startActivity(localSMS); 
      } 
     } 
     else{ 
      Log.d("MSG", "intent.getAction() != android.provider.Telephony.SMS_RECEIVED"); 
     } 
    } 
} 

如果活動是在前臺運行,一切都很正常。如果活動在後臺運行(服務和通知已創建),則context.startActivity(localSMS);將在SMS到達時執行,但活動的onNewIntent()方法不會被調用,活動也不會被帶到前臺。

如果我在執行context.startActivity(localSMS);後的某個時候手動單擊通知,那麼活動將進入前臺(如預期的那樣),現在執行onNewIntent()

我想要發生的是,如果活動在後臺運行(服務和通知創建),那麼在SMS到達後,我的接收器將把活動帶到前臺(通過startActivity(localSMS);),然後,該活動將調用其方法onNewIntent()

我不知道什麼是錯的。我認爲服務和接收者彼此衝突,但我可能完全不在。對不起,這很久了。我通常可以在這裏找到我的問題的答案,但對於這個問題我沒有運氣。任何幫助是極大的讚賞!

回答

0

我想清楚丟失了什麼。我的服務中沒有接收器:我的服務等級更改爲:

public class SMSService extends Service{ 

    SMSReceiver receiver = new SMSReceiver(); 

    @Override 
    public void onCreate() { 
     Log.d("MSG", "SMSService OnCreate Started"); 

     createNotification(); 
     this.registerReceiver(receiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED")); 
     super.onCreate(); 
     Log.d("MSG", "SMSService OnCreate Ended"); 
    } 

    public class MyBinder extends Binder {... 
相關問題