2016-02-04 61 views
-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。我不知道这是巧合还是不巧。 感谢您的帮助

+0

什么这与Android的工作室呢? – thedarkpassenger

+0

我有一个数据库,节省用户的langitude和longitutude。该服务每5分钟上传一次 – Ugur

回答

0

请检查了这一点
这是我的服务,让协调每隔15秒,并将其保存到SQLITE,把所有保存的坐标服务器每5分钟发送一个完整的坐标+解决服务器每10分钟
不介意onDestroy方法的内容
这只是我的私人图书馆

FYI:在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; 
} 
}