-2
这是我的代码 MyService.java服务有时不工作
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
class BackgroundTask extends AsyncTask<String,Void,String>
{
//My database upload code
}
}
private void ping() {
try {
BackgroundTask backgroundTask = new BackgroundTask();
backgroundTask.execute(); //Running asynctask
} catch (Exception e) {
}
scheduleNext();
}
private void scheduleNext() {
mHandler.postDelayed(new Runnable() {
public void run() {
ping();
}
}, 300000);
}
public int onStartCommand(Intent intent, int x, int y) {
mHandler = new android.os.Handler();
ping();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
autostart.java
public class autostart extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent=new Intent(context,MyService.class);
context.startService(intent);
}
}
在AndroidManifest.xml我有这些:
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<receiver android:name=".autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyService"
android:enabled="true"
>
</service>
我有时在logcat中看到这个
02-04 19:57:25.125 10329-10377/com.example.pc.googlemaps I/GMPM: App measurement is starting up
02-04 19:57:25.135 10329-10377/com.example.pc.googlemaps E/GMPM: getGoogleAppId failed with status: 10
02-04 19:57:25.135 10329-10377/com.example.pc.googlemaps E/GMPM: Uploading is not possible. App measurement disabled
此服务有时无法正常运行。我从手机上检查应用程序管理器,我发现它运行,但正如我所说有时我的代码工作有时不起作用。例如,当我切换我的手机新,我检查应用程序管理器和我的程序服务运行。我无法解决问题在哪里。 我的意思是,它似乎MyService总是运行在应用程序管理器,但代码有时运行有时不运行。 我注意到,当我从手机上检查应用程序管理器时,如果MyService是4-5 MB,它不运行,如果它运行13-15 MB。我不知道这是巧合还是不巧。 感谢您的帮助
什么这与Android的工作室呢? – thedarkpassenger
我有一个数据库,节省用户的langitude和longitutude。该服务每5分钟上传一次 – Ugur