2
我正在製作一個跟蹤用戶位置的應用程序,到目前爲止,我已經能夠成功接收其位置更改的座標,但是如果他重新啓動手機比我無法啓動我的服務而無需用戶再次打開應用程序。如何在Android設備重啓時啓動我的應用程序進程?
這是我Servicesstart.java代碼
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i("service started", "start");
userFunctions = new UserFunctions();
final LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(userFunctions.isUserLoggedIn(getApplicationContext())){
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
imei=telephonyManager.getDeviceId();
Toast.makeText(getApplicationContext(), imei, 5000).show();
if(Broadcast.check==false)
{
final UserFunctions userFunction = new UserFunctions();
LocationListener locatioListner = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
get();
}
public void onLocationChanged(Location location) {
Log.i("location", "loc");
String latitude=String.valueOf(location.getLatitude());
String longtitude=String.valueOf(location.getLongitude());
//location updated starts here
boolean check_update=userFunction.locationUpdater(UserFunctions.email, latitude, longtitude,imei);
if(check_update==true){
Log.i("updated", "update");
}
else
{
Log.i("notupdated","not");
}
}
};
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locatioListner);
}
}
}
這是我Broadcast.java代碼
public class Broadcast extends BroadcastReceiver {
public static boolean check=true;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
check=false;
Intent i = new Intent(context,Servicestart.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(i);
}
}
}
這是我的manifest.xml代碼
<service android:name=".Servicestart">
</service>
<receiver android:name=".Broadcast"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
如何當一個應用程序在您的手機或PC未經您明確同意啓動什麼感覺? –
嘗試放置斷點或登錄onReceive方法,並檢查是否可以捕獲事件。可能您的接收器正在工作,但您的意圖存在上下文問題。 –
哦,你添加了它的權限? <使用權限android:name =「android.permission.RECEIVE_BOOT_COMPLETED」/> –