gpt4 book ai didi

android - 无法通过此代码接收消息(广播接收器)

转载 作者:行者123 更新时间:2023-11-29 20:49:29 24 4
gpt4 key购买 nike

我没有通过我的应用程序收到消息 - 帮助我,我不知道我必须在此代码中进行哪些更改,请告诉我我应该将其设置为默认值(这样它会给我一条消息)还是在我的代码 - 我确实收到了一条消息,但不是来自这个应用程序

    public class IncomingSms extends BroadcastReceiver {

final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

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

try {

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

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

SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage
.getDisplayOriginatingAddress();

String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();

Log.i("SmsReceiver", "senderNum: " + senderNum
+ "; message: " + message);

// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: "
+ senderNum + ", message: " + message, duration);
toast.show();

} // end for loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);

}
}

}

list .xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.smsmanager"
android:versionCode="1"
android:versionName="1.0" >

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

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

<application
android:allowBackup="true"
android:icon="@drawable/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>
</application>

最佳答案

我认为,你应该添加这段代码

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

在 list 中的应用程序标签中

或者您必须在 Activity 类中注册您的 InComingservice。

例如

private InComingSMSReceiver inComingSMSReceiver = new InComingSMSReceiver();
@Override
protected void onResume() {
super.onResume();
registerReceiver(inComingSMSReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
}

@Override
protected void onPause() {
super.onPause();
unregisterReceiver(inComingSMSReceiver);
}

关于android - 无法通过此代码接收消息(广播接收器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29532413/

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