2017-10-14 181 views
0

與此How do I start my app on startup?運行後的應用時成功色器件功率如何在啓動後顯示應用程序屏幕時在後臺運行應用程序?

但是當運行色器件這表明我的main_activity

我想要運行的應用程序在後臺(因爲我調用帶有計時器凌空請求在後臺檢查服務器)

我的manifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <receiver 
     android:enabled="true" 
     android:exported="true" 
     android:name="other_class.CLASS_START_UP" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 

     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

    </receiver> 

我的課START_UP:

public class CLASS_START_UP extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
     Intent i = new Intent(context, MainActivity.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(i); 
    } 
} 
} 

我用這個貼子Run volley request every 5 minutes in background android運行的計時器凌空要求

我的計時器代碼:

private Runnable runnableCode = new Runnable() { 
    @Override 
    public void run() { 

     get_start_info(); 

     // Repeat this the same runnable code block again another 2 seconds 
     handler.postDelayed(runnableCode, 10000); 
    } 
}; 

我的問題是,我不想上節目的主要活動表演時色器件轉向用戶(前景)我想只在後臺運行

很抱歉,我的英語也不好

回答

2

試試這個

<receiver android:name=".service.ShutdownReceiver" 
     > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      <action android:name="android.intent.action.REBOOT"/> 
      <action android:name="android.intent.action.ACTION_SHUTDOWN" /> 
      <action android:name="android.intent.action.QUICKBOOT_POWERON"/> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 

ShutdownReciver.java

public class ShutdownReceiver extends BroadcastReceiver 
    { 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
    Log.e("Brodcast Call","####"); 
    Toast.makeText(context, "service", Toast.LENGTH_SHORT).show(); 
    //   context.startService(new Intent(context, 
    PowerButtonService.class)); 
    Intent myIntent = new Intent(context, PowerButtonService.class); 
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(myIntent); 


} 

}