2016-02-04 92 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; 
} 
}