gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:32:06 28 4
gpt4 key购买 nike

我知道从 KitKat 开始,我们需要将应用设为默认短信应用,以便能够修改短信数据库。

但我不想这样。我只需要收到传入短信的通知并显示为 toast,还发送短信

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

编辑 1:这是我的 list 文件:

<?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>

这是我的广播接收器:

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) {

}
}
}
}

最佳答案

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

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

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

关于android - 如何在 Android KitKat 或更高版本上接收传入的短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35587013/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com