应用程序在Android工作室的模拟器中正常工作但在真实设备上没有 - BroadcastReceiver

时间:2015-10-20 06:42:08

标签: android

这是一个提供收到的消息并给予回复的应用程序 当我使用模拟器测试它工作正常但不在设备上

-----这是MainActivity.java ---------

 package com.example.yj.example;

 import android.app.Activity;
 import android.os.Bundle;
 import android.support.v7.app.ActionBarActivity;
 import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}

-------这是SmsReceiver.java -----------

 package com.example.yj.example;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.telephony.SmsManager;
 import android.telephony.SmsMessage;
 import android.widget.Toast;

 public class SmsReceiver  extends BroadcastReceiver {
 @Override
public void onReceive(Context context, Intent intent) {

    Bundle bundle=intent.getExtras();

    if(bundle != null){
        Object[] pdus= (Object[]) bundle.get("pdus");
        String senderNumber=null;
        for(int i=0; i<pdus.length; i++){
            SmsMessage sms=SmsMessage.createFromPdu((byte[]) pdus[i]);

            senderNumber = sms.getDisplayOriginatingAddress();

            String message= sms.getDisplayMessageBody();

            Toast.makeText(context, "From:" + senderNumber + "  Message:" +         message, Toast.LENGTH_LONG).show();
        }

        SmsManager smsManager=SmsManager.getDefault();
        smsManager.sendTextMessage(senderNumber,null,"u r lucky!!!!! its finally working",null,null);
    }
}
 }

---------这是AndroidManifest.xml -----------------

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.yj.example" >

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


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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>

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

  </application>

 </manifest>

0 个答案:

没有答案