如何在收到短信时使用广播接收器进行祝酒?

时间:2016-10-07 13:34:18

标签: android broadcastreceiver sms

这是MainActivity.java

package tagit.aj.com.broadcastreceiverforsms;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

MyReceiver类包含广播接收器的onReceive方法。这里使用的方法之一是"已弃用"但我认为它不会在测试中产生任何问题。

package tagit.aj.com.broadcastreceiverforsms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context,"IncomingSms",Toast.LENGTH_SHORT).show();

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdus.length; i++) {

                    SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[i]);

                    String phoneNumber = messages.getDisplayOriginatingAddress();
                    String phone = phoneNumber;
                    String stringMessage = messages.getDisplayMessageBody();

                    int duration = Toast.LENGTH_LONG;
                    Log.i("Broadcasting", "Number" + phone + "Message" + stringMessage);
                    Toast toast = Toast.makeText(context, "senderNum: " + phone + ", message: " + stringMessage, duration);
                    toast.show();

                } // end for loop
            } // bundle is null
        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);
        }
    }
}

XML布局是:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="tagit.aj.com.broadcastreceiverforsms.MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </RelativeLayout>

AndroidManifest文件是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tagit.aj.com.broadcastreceiverforsms">
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

请告诉我为什么会出现这个问题,以及解决方案(如果有的话)。我在Android KitKat上运行此应用程序。

1 个答案:

答案 0 :(得分:0)

我认为他的问题是由于Android 4.4中的SMS提供程序所做的更改,他的代码无效。

SMS Provider in Android 4.4