开始重新启动后意图 [英] Start intent after Reboot
本文介绍了开始重新启动后意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个GPS服务,获取GPS位置每60秒。它的工作不错,但它不会做手机重启后的任何东西。我想在一个广播接收器
正在重新启动,但没有工作后发生添加此。任何帮助将是巨大的;我只需要重新启动后加载我的意图。
//开始重新启动后意图
如果(intent.getAction()。等于(android.intent.action.BOOT_COMPLETED)){
context.startService(新意图(上下文,DashboardActivity.class));
}
GPSActivity.java
公众诠释onStartCommand(意向意图,诠释旗帜,INT startId){ //Toast.makeText(getBaseContext(),服务启动,Toast.LENGTH_SHORT).show(); 最终的Runnable R =新的Runnable(){
公共无效的run(){
Log.v(GPS_TRACKER,运行启动);
位置();
handler.postDelayed(这一点,60000);
}
};
handler.postDelayed(R,60000);
返回START_STICKY;
}
清单
<! - GPS活动 - >
<服务机器人:启用=真正的机器人:名字=GPSActivity。>
&所述;意图滤光器>
<作用机器人:名字=com.ni.androidtrack.GPSActivity/>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
&所述; /意图滤光器>
< /服务><! - 也许可项值 - >
<使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>
解决方案
在你的清单:
<服务机器人:出口=false的机器人:名字=机器人service.YourService:启用=真正的>< /服务>
<接收机器人:名字=。service.YourBootReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.BOOT_COMPLETED/>
&所述; /意图滤光器>
< /接收器>
此外,在清单中添加权限:
<使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>
YourBootReceiver:
公共类YourBootReceiver扩展广播接收器{ @覆盖
公共无效的onReceive(上下文为arg0,ARG1意向){
意图serviceIntent =新意图(arg0中,YourService.class);
arg0.startService(serviceIntent);
}
I have a GPS Service that gets GPS position every 60 seconds. It's working okay, but it doesn't do anything after phone reboot. I tried adding this in a BroadcastReceiver
that is working after reboot but nothing happens. Any help would be great; I just need to load my Intents after reboot.
//Start intents after reboot
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
context.startService(new Intent(context, DashboardActivity.class));
}
GPSActivity.java
public int onStartCommand(Intent intent, int flags, int startId) {
//Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT).show();
final Runnable r = new Runnable() {
public void run() {
Log.v("GPS_TRACKER", "Run Start");
location();
handler.postDelayed(this, 60000);
}
};
handler.postDelayed(r, 60000);
return START_STICKY;
}
Manifest
<!-- GPS Activity -->
<service android:enabled="true" android:name=".GPSActivity">
<intent-filter>
<action android:name="com.ni.androidtrack.GPSActivity"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</service>
<!-- Also permission for RECEIVE_BOOT_COMPLETED -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
解决方案
In your manifest:
<service android:exported="false" android:name=".service.YourService" android:enabled="true"></service>
<receiver android:name=".service.YourBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Also add permission in manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
YourBootReceiver:
public class YourBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Intent serviceIntent = new Intent(arg0, YourService.class);
arg0.startService(serviceIntent);
}
这篇关于开始重新启动后意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文