gpt4 book ai didi

java - 传入短信内容

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:16 25 4
gpt4 key购买 nike

我希望我能够获取传入短信的内容,但它不起作用..事实上,不会出现任何toast,日志中也不会出现任何内容,我认为myreceiver没有初始化......所以这里是 list 、onCreate 第一个 Activity 和我的接收器 java 文件: list :

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

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

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

<application
android:allowBackup="true"
android:icon="@mipmap/locate"
android:label="@string/app_nameprincipal"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Changelog"
android:label="@string/title_activity_changelog"
android:theme="@style/AppTheme.NoActionBar" />

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBfmF3WWxsPhufZR5keiDNRy-33hJI1rvM" />

<activity
android:name=".lequipe"
android:label="@string/nomequipe" />
<activity
android:name=".Setting"
android:label="@string/title_activity_setting" />
<activity android:name=".MDPinterne" />
<activity android:name=".Bluetooth"></activity>
</application>

</manifest>

创建时:

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

ActionBar actionBar = getSupportActionBar();
actionBar.hide();
MyReceiver sms = new MyReceiver();
Log.v("aha3", "smslancer");

和java文件:

public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {


Bundle extra = intent.getExtras();
if (extra != null) {
Object[] pdus = (Object[]) extra.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
if (messages.length > -1) {
Log.v("Ch", "Marche1");
for (int i = 0; i < messages.length; i++) {
Log.v("Ch", "Marche2");
final String messageBody = messages[i].getMessageBody();
final String phoneNumber = messages[i].getDisplayOriginatingAddress();
Toast.makeText(context, "Expéditeur:" + phoneNumber, Toast.LENGTH_LONG).show();
Toast.makeText(context, "Message : " + messageBody, Toast.LENGTH_LONG).show();
}
}
}

}

}

最佳答案

将 list 中的接收器 XML 移至应用程序标记内。

<application
android:allowBackup="true"
android:icon="@mipmap/locate"
android:label="@string/app_nameprincipal"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

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

关于java - 传入短信内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36608532/

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