这是我的代码 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
此服务有时无法正常运行。我从我的手机上检查应用程序管理器,我发现它运行但正如我所说,有时我的代码工作有时不起作用。例如,当我打开手机时,我检查App Manager并运行我的程序服务。我无法解决问题所在。 我的意思是,似乎MyService总是在App Manager中运行,但有时运行的代码有时不会运行。 我注意到,当我从我的手机上检查App Manager时,如果MyService是4-5 MB,它就不会运行,如果它运行13-15 MB。我不知道这是巧合。 谢谢你的帮助
答案 0 :(得分:0)
请检查出来
这是我的服务,每隔15秒获取一次坐标并将其保存到SQLITE,每隔5分钟将所有保存的坐标发送到服务器,并每隔10分钟向服务器发送一个完整的坐标+地址。
不要介意onDestroy
方法的内容
它只是我的私人图书馆
仅供参考:不要在onCreate
方法中使用处理程序,这会导致您的应用无法响应
相信我,我在尝试之前尝试过
而是使用AlarmManager
来获得更好的性能
public class service extends Service{
private AlarmManager alarm;
private PendingIntent addData, sendData, fullData;
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
if(!App.User.GetUserCode().equals("")){
App.DataFrame.InfoLog("Location is turned OFF");
App.DataFrame.Off();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
stopAlarm();
Intent stoploc = new Intent(service.this, LocationProvider.class);
service.this.stopService(stoploc);
stopForeground(true);
stopSelf();
App.User.Logout();
App.Toast("Stopping Service");
}
}, 1000);
}else{
stopAlarm();
Intent stoploc = new Intent(service.this, LocationProvider.class);
service.this.stopService(stoploc);
stopForeground(true);
stopSelf();
App.Toast("Stopping Service");
}
super.onDestroy();
}
public void stopAlarm(){
alarm.cancel(addData);
alarm.cancel(sendData);
alarm.cancel(fullData);
}
@Override
public void onCreate() {
App.Toast("Starting Services");
alarm = (AlarmManager) getSystemService(App.GetContext().ALARM_SERVICE);
Intent intent = new Intent(this, CoorReceiver.class);
addData = PendingIntent.getBroadcast(this, 0, intent.putExtra("Code", 0), PendingIntent.FLAG_UPDATE_CURRENT);
sendData = PendingIntent.getBroadcast(this, 1, intent.putExtra("Code", 1), PendingIntent.FLAG_UPDATE_CURRENT);
fullData = PendingIntent.getBroadcast(this, 2, intent.putExtra("Code", 2), PendingIntent.FLAG_UPDATE_CURRENT);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, 15000, 15000, addData);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, 30000, 30000, sendData);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, 600000, 600000, fullData);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent intent2 = new Intent(this, MainActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent2, 0);
Notification noti = new Notification.Builder(getApplicationContext())
.setContentTitle("Imagine")
.setContentText("Service is Running")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendIntent)
.build();
startForeground(1234, noti);
return START_STICKY;
}
}