gpt4 book ai didi

java - 广播接收器未启动服务

转载 作者:行者123 更新时间:2023-11-30 04:44:26 25 4
gpt4 key购买 nike

预先感谢您的所有帮助。我对 Android 和编程世界还很陌生,正在寻找一些指导。我正在尝试收听一条短信,然后启动一项服务以在后台运行。我知道广播接收器正在工作,因为当我向模拟器发送文本时会弹出 Toast 通知,仅此而已。在那之后,我没有从服务中的 Toast 收到任何东西。

这是我的广播接收器

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {
protected String className = this.getClass().getName();
protected int toastLength = Toast.LENGTH_SHORT;

@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();

Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}

// show first message
Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
Intent serviceIntent = new Intent(context, MyService.class);
serviceIntent.setAction("SMS.example.MyService");
context.startService(serviceIntent);
}
}

我弹出 toast 通知,但在收到文本后它没有启动服务。这是服务的代码:

public class MyService extends Service {
final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
BroadcastReceiver SMSReceiver = new BroadcastReceiver(){
protected String className = this.getClass().getName();
protected int toastLength = Toast.LENGTH_SHORT;

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, className, toastLength).show();
Log.i("INCOMINGSMSRECEIVER", intent.getAction());

if (intent.getAction().equals(SMS_RECEIVED)) {
SmsManager sms = SmsManager.getDefault();

Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i=0; i<pdus.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

for (SmsMessage message : messages) {
String msg = message.getMessageBody();
String to = message.getOriginatingAddress();
//sms.sendTextMessage(to, null, "Got It" + msg, null, null);
Log.i("INCOMINGSMSRECEIVER", "SMS received.");

}
}
}
}
};

//@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate();

Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
IntentFilter filter = new IntentFilter(SMS_RECEIVED);
registerReceiver(SMSReceiver, filter);

}
}

抱歉所有的代码,只是想给你们所有的信息。最后是 Mainfest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="SMS.example"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".MyBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service android:name=".MyService" android:enabled="true">
<intent-filter>
<action android:name="SMS.example.MyService" />
</intent-filter>
</service>

谁能告诉我为什么我的服务没有启动?

最佳答案

我认为这是因为您没有使用 onStartCommand()读这个:http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int)

请注意 onStart() 已弃用,应改用 onStartCommand()。

关于java - 广播接收器未启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5314840/

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