我有一个项目只是一个服务,它没有活动和用户界面。我想在手机完全启动时启动我的应用程序后台服务。但我从来没有收到操作系统的“BOOT_COMPLETED”消息。这是我的代码:为什么Broadcast Receiver不能用于服务应用程序android?
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droid.arghaman.location_tracker">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</receiver>
</application>
<service android:name=".mySevice"></service>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
广播接收器:
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("boot Received", intent.getAction());
Intent serviceLuncher = new Intent(context, myService.class);
context.startService(serviceLuncher);
}
}
为myService:
public class LocationNotifierService extends Service {
Timer timer ;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate(){
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Toast.makeText(getBaseContext(),"Location",Toast.LENGTH_SHORT).show();
}
},3000);
}
@Override
public void onDestroy(){
}
@Override
public int onStartCommand(Intent intent, int flagId, int startId){
return START_STICKY;
}
}
,但我从来没有得到“启动接收”日志。 是否有任何错误,并有任何方法来调试我的程序?
我建议我的项目必须只有这个服务,它不能有任何的UI。
我有接收器在我的清单.... –
请提供解决方案...我如何开始我的服务而无需用户交互??? –
@ Navid_pdp11:一般没有解决方案。 Android的设置是为了防止恶意软件作者做你正在做的事:隐藏用户并窥探他们。 – CommonsWare