我尝试创建在接收短信时运行服务的简单应用。 我写了所有的代码,我仍然看不到任何在短信接收上运行的服务。
代码
服务
public class MainService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "onStartCommand", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
}
BroadcastReceiver类
public class SmsReceiver extends BroadcastReceiver {
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MainService.class));
// TODO - complete the sms analyze
}
}
AndroidManifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service android:name=".MainService"/>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
答案 0 :(得分:0)
I also suffered this such issue earlier. I solved the same by using the flag android:enabled="true".
<receiver android:name=".SmsReceiver" android:enabled="true"
android:priority="2000000" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
实际上默认会启用它。我不知道以后会如何改变。请参阅文档http://developer.android.com/guide/topics/manifest/receiver-element.html
否则你可以再做一件关于优先考虑的事情。你能否使用大于1000的优先级。 示例: - android:priority =&#34; 2000000&#34;。
在您的Android设备中,您拥有具有更高优先级接收器的短信应用程序。当他们到达时他们可能阻止短信。