如何在Android KitKat或更高版本上接收传入的短信?

时间:2016-02-23 19:58:51

标签: android sms default android-4.4-kitkat

我从KitKat开始知道我们需要将应用程序作为默认的SMS应用程序来修改SMS数据库。

但我不希望如此。我只需要收到传入短信的通知并显示为祝酒词,还发送短信

我是否需要将我的应用设为默认值?

编辑1: 这是我的清单文件:

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

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true">
        <activity
            android:name=".SplashScreen"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LoginScreen" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"/>
        <activity android:name=".MainScreen" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        </activity>

        <service android:name=".MainService"
            android:exported="true">
        </service>

        <receiver android:name=".MainReceiver"
            android:exported="true"
            android:enabled="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

这是我的BroadcastReceiver:

package com.rtrgroup.sms_to_neta_server;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.widget.Toast;
import android.telephony.*;

public class MainReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
        Bundle bundle = intent.getExtras();
        try {
            if (bundle != null) {
                Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage currentMessage;
                    if (Build.VERSION.SDK_INT >= 19) { //For ver. >= KitKat
                        SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
                        currentMessage = msgs[0];
                    } else {
                        Object pdus[] = (Object[]) bundle.get("pdus");
                        currentMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);
                    }

                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String messageB1 = currentMessage.getDisplayMessageBody();
                    String msg = currentMessage.getMessageBody();
                    String address = currentMessage.getOriginatingAddress();

                    Toast.makeText(context, senderNum + ": " + msg, Toast.LENGTH_SHORT).show();
                }
            }
        } catch (Exception e) {

       }
     }
   }
}

1 个答案:

答案 0 :(得分:1)

您无需成为发送短信的默认短信应用。只要您拥有SmsManager.sendTextMessage()权限,android.permission.SEND_SMS仍然可以使用。

要接收短信,您可以android.provider.Telephony.SMS_RECEIVED权限监控android.permission.RECEIVE_SMS广播。

参考。 http://android-developers.blogspot.fi/2013/10/getting-your-sms-apps-ready-for-kitkat.html