2011-03-15 308 views
1

非常感謝您的幫助。我對Android和編程界還是很新的,並且正在尋找一些指導。我試圖讓一條文本消息被監聽,然後啓動一個服務在後臺運行。我知道廣播接收器正在工作,因爲當我向模擬器發送文本時,彈出了Toast通知,但就是這樣。此後我從服務中得到的吐司中沒有收到任何東西。廣播接收器未啓動服務

這裏是我的廣播接收器

import android.content.BroadcastReceiver; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.os.Bundle; 
import android.telephony.gsm.SmsMessage; 
import android.widget.Toast; 

public class MyBroadcastReceiver extends BroadcastReceiver { 
protected String className = this.getClass().getName(); 
protected int toastLength = Toast.LENGTH_SHORT; 

@Override 
public void onReceive(Context context, Intent intent) { 
    Bundle bundle = intent.getExtras(); 

    Object messages[] = (Object[]) bundle.get("pdus"); 
    SmsMessage smsMessage[] = new SmsMessage[messages.length]; 
    for (int n = 0; n < messages.length; n++) { 
     smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); 
    } 

    // show first message 
    Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG); 
    toast.show(); 
    Intent serviceIntent = new Intent(context, MyService.class); 
    serviceIntent.setAction("SMS.example.MyService"); 
    context.startService(serviceIntent); 
    } 
} 

我得到的Toast通知彈出,但收到的文本之後,在沒有啓動該服務。下面是服務的代碼:

public class MyService extends Service { 
final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; 
BroadcastReceiver SMSReceiver = new BroadcastReceiver(){ 
protected String className = this.getClass().getName(); 
protected int toastLength = Toast.LENGTH_SHORT;  

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, className, toastLength).show(); 
     Log.i("INCOMINGSMSRECEIVER", intent.getAction()); 

     if (intent.getAction().equals(SMS_RECEIVED)) { 
      SmsManager sms = SmsManager.getDefault(); 

      Bundle bundle = intent.getExtras(); 
      if (bundle != null) { 
       Object[] pdus = (Object[]) bundle.get("pdus"); 
       SmsMessage[] messages = new SmsMessage[pdus.length]; 
       for (int i=0; i<pdus.length; i++) 
        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 

       for (SmsMessage message : messages) { 
        String msg = message.getMessageBody(); 
        String to = message.getOriginatingAddress(); 
        //sms.sendTextMessage(to, null, "Got It" + msg, null, null); 
        Log.i("INCOMINGSMSRECEIVER", "SMS received."); 

       } 
      } 
     }   
    } 
}; 

    //@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(); 

    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show(); 
    IntentFilter filter = new IntentFilter(SMS_RECEIVED);  
    registerReceiver(SMSReceiver, filter); 

} 
} 

對不起,所有的代碼,只是想給你們的所有信息。最後這裏是Mainfest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="SMS.example" 
    android:versionCode="1" 
    android:versionName="1.0"> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission> 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<application android:icon="@drawable/icon" android:label="@string/app_name">  
<receiver android:name=".MyBroadcastReceiver" android:enabled="true"> 
    <intent-filter> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter> 
</receiver> 
<service android:name=".MyService" android:enabled="true"> 
    <intent-filter> 
     <action android:name="SMS.example.MyService" />  
    </intent-filter> 
</service>  

誰能告訴我,爲什麼沒有被開始了我的服務?

回答

0

我以爲我會保留這些代碼給其他人,但我找到了問題。我沒有爲onCreate()彈出乾杯,但onStart()方法會彈出一個乾杯!希望這有助於他人。