我是法国人,对不起我的英语。我正在尝试开发一个在后台运行并捕获 SMS 的自动启动服务。经过大量研究,我无法解决我的问题。
开机后无法运行服务,收不到日志
Log.v("LTM","MyReceiver.onReceive: "+intent.getAction());
应该由 BootReceiver 打印。(下面的CF代码)
我验证了 AndroidManifest,测试了不同的代码,但没有任何工作正常。我使用 Eclipse 并使用 Android 虚拟设备测试代码,所以问题可能来自模拟器,我使用真实设备进行了测试,但它也没有启动。
您可以在下面找到代码。谢谢您的帮助。
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smsmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<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" >
<service android:enabled="true" android:label="ServiceGUI" android:name="com.smsmanager.MainService"/>
<receiver android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver class="com.smsmanager.SMSReceiver" android:name="com.smsmanager.SMSReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
主要的:
package com.smsmanager;
import com.smsmanager.SMSReceiver;
import android.os.IBinder;
import android.util.Log;
import android.app.Service;
import android.content.Intent;
public class MainService extends Service {
SMSReceiver rec;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("LTM", "Demarrage du service");
rec=new SMSReceiver();
Log.v("LTM", "Retour dans le service");
return super.onStartCommand(intent, flags, startId);
}
}
接收者:
package com.smsmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
// TODO Auto-generated method stub
Log.v("LTM","MyReceiver.onReceive: "+intent.getAction());
Intent intent1=new Intent(ctx,MainService.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startService(intent1);
}
}