提前感谢您的帮助。我对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>
有谁能告诉我为什么我的服务没有开始?
答案 0 :(得分:1)
我认为这是因为你没有使用onStartCommand() 请阅读:http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int)
被告知不推荐使用onStart(),而应该使用onStartCommand()。
答案 1 :(得分:0)
我以为我会为其他人保留这些代码,但我发现了问题。我没有为onCreate()弹出toast,但onStart()方法会弹出一个toast!希望这有助于其他人。