1

我有一个小问题。我无法让我的广播接收器显示任何 toast 消息。也许我无法以某种方式触发它,但我的发件人可以工作。我想知道它是否甚至触发。

主班

 public class Main_Activity extends Activity {


Button buttonSend;
EditText textPhoneNo;
EditText textSMS;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   Button  buttons = (Button) findViewById(R.id.gett);
    buttonSend = (Button) findViewById(R.id.buttonSend);
    textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
    textSMS = (EditText) findViewById(R.id.editTextSMS);
    EditText edt = (EditText)findViewById(R.id.reciving);


    buttons.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
        //  EditText edt = (EditText)findViewById(R.id.reciving);
        //  SmsReceiver obj = new SmsReceiver();
        //  String test = obj.number2;
        //    edt.setText("you stupid result "+test);

        }
    });

    SmsReceiver obj = new SmsReceiver();
//  String test = obj.number2;
 //   edt.setText("you stupid result "+test);





  buttonSend.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

        String phoneNo = textPhoneNo.getText().toString();
        String sms = textSMS.getText().toString();

        try{
            SmsManager  smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, sms, null, null);


        }catch (Exception e)
        {
            System.out.println("Not send");
        }





    }
})  ;


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
 }

短信接收器类:

  public class SmsReceiver extends BroadcastReceiver
 {
@Override
public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";            
    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]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";        
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }                         
}
 }

主播

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dom.example.sms_exp"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Main_Activity"
        android:label="@string/title_activity_main_" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".SmsReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

    <activity
        android:name=".SmsReceiver"
        android:label="@string/title_activity_send_activity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

请帮忙,我明天有一个项目要提交。

4

1 回答 1

0

如果您希望您的应用程序接收 SMS 广播,那么您必须在 manifast 中包含以下权限

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

并将您的 SmsReceiver 代码更改为:

public class SmsReceiver extends BroadcastReceiver
 {
  @Override
    public void onReceive(Context context, Intent intent) 
    {
       if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
           {
              //put your code here....
           }
       else{
             // do something here...
           }
    }
}
于 2012-11-11T17:34:44.903 回答