gpt4 book ai didi

java - Android:从服务接收和转发短信

转载 作者:行者123 更新时间:2023-11-30 01:20:43 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的应用程序,它将所有具有特定正文的消息转发到另一个号码。

我类的广播接收器没有被调用。任何线索将不胜感激

主 Activity .java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Settings.Applog("App started !!");
Intent intent = new Intent(this, SMSService.class);
startService(intent);
}

短信服务.java

public class SMSService extends Service {

private IntentFilter mIntentFilter;
private SMSreceiver mSMSreceiver;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate()
{
super.onCreate();
Settings.Applog("Service started !!");
//SMS event receiver
mSMSreceiver = new SMSreceiver();
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(mSMSreceiver, mIntentFilter);
}

@Override
public void onDestroy()
{
super.onDestroy();

// Unregister the SMS receiver
unregisterReceiver(mSMSreceiver);
}

private String ConvertNumber(String from){
return from;
}

public class SMSreceiver extends BroadcastReceiver
{

private final String TAG = this.getClass().getSimpleName();

@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
String strMessage = "";
Settings.Applog("Got a message!!");
if ( bundle != null )
{
try{
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
Settings.Applog("Message body ::"+msgBody);
}
}catch(Exception e){
//Log.d("Exception caught",e.getMessage());
}

}
}
}

list .xml

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

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

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

<service
android:name=".SMSService"
android:exported="false">
</service>

</application>

</manifest>

最佳答案

您必须在 list 文件中注册用于接收 SMS 的广播接收器。只是权限是行不通的。

在 application 标签内,它看起来像这样:

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" >
<receiver android:name=".SMSBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>

</application>

以接收短信为例。它应该是一个很好的起点。 http://javapapers.com/android/android-receive-sms-tutorial/

关于java - Android:从服务接收和转发短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37147665/

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