2013-12-20 34 views

回答

5

首先活動不能也不應該在後臺工作。對於Android中的後臺操作,您應該使用Service,但可以通過以下代碼隱藏活動。

第一步: 設置權限在AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

第二步: 添加這是接收意圖過濾器,

<receiver android:name=".BootReciever"> 
    <intent-filter > 
     <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 

    </intent-filter> 
</receiver> 

第三步: 現在你就可以開始你的應用程序的第一項活動從接收器類的onReceive方法。

public class BootReciever extends BroadcastReceiver 
{ 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    Intent myIntent = new Intent(context, MainActivity.class); 
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    myIntent.addCategory(Intent.CATEGORY_HOME); 
    context.startActivity(myIntent); 
} 

} 

和您的活動的onCreate把這個

moveTaskToBack(true); 

編輯:FOR服務使用此

public class BootReciever extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent myIntent = new Intent(context, service.class); 
     context.startService(myIntent); 
    } 
} 


public class service extends Service 
{ 
    private static final String TAG = "MyService"; 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 
    public void onDestroy() { 
     Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onDestroy"); 
    } 

    @Override 
    public void onStart(Intent intent, int startid) 
    { 

     Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
     Log.d(TAG, "onStart"); 
    } 
} 

這行添加到您的Androidmanifeist.xml

<service android:enabled="true" android:name=".service" /> 

對於更多信息看這裏

http://blog.vogella.com/2011/12/11/automatically-starting-services-in-android-after-booting/

http://www.vogella.com/articles/AndroidServices/article.html

+0

什麼是Tabs.class在你的代碼在這裏?請澄清。謝謝 –

+0

這是我想要啓動的活動,在執行moveTaskToBack(true)之後它會自動轉到後臺。 – Hardik

+0

謝謝。但我的應用程序如何作爲服務在後臺運行?我嘗試在BootReciever類中包含Toast消息,但它只出現一次,持續幾秒鐘。任何好主意或想法? –

2

要啓動設備上啓動(在你的清單):

<receiver android:name="MyBroadcastReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"></action> 
      <category android:name="android.intent.category.HOME"></category> 
     </intent-filter> 
</receiver> 

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"> 
</uses-permission>   
<uses-permission android:name="android.permission.REBOOT" /> 

爲了保持運行:我不知道這是否是可能的,因爲用戶可以停止從設置應用程序菜單...

+0

如果用戶想停止該應用程序,那麼它對我來說很好。我想要的只是在智能手機啓動時讓我的應用程序在後臺運行。任何線索? –

3

Activity不能也不應該在後臺工作。對於Android中的後臺操作,您應該使用Service

如需更多幫助,檢查該鏈路

Running activity in the background

有關引導加載啓動服務以下代碼 -

添加permissions-

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

添加接收機 -

<receiver 
    android:name="com.<!your activity!>" 
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

在你的活動接收機類

public class MyReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    Intent myIntent = new Intent(context, YourService.class); 
    context.startService(myIntent); 
    } 

對於長時間運行的後臺操作服務,應使用。

1

您需要使用由BroadcastReceiver觸發的服務。

添加權限

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"> 

的AAA BroacastReceiver檢查時,系統啓動完畢或當設備關閉。 的廣播接收器被聲明爲:

<receiver android:name="AAA"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      <action android:name="android.intent.action.ACTION_SHUTDOWN"/> 
      </intent-filter> 
    </receiver> 

這裏是廣播接收器

public class AAA extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

    String action = intent.getAction(); 
    Intent i = new Intent(context, BackgroundService.class); 
    if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { 

     // The boot is completed - > Start the service 

     context.startService(i); 


    } else if (action.equals(Intent.ACTION_SHUTDOWN)) { 

     // The device is shutting down - Stop the service. 
     context.stopService(i); 
    } 



    } 


} 

的實現當引導正確完成,廣播開始的服務;而當設備關機通知廣播AAA時,服務停止。

該服務需要是一個粘性(如果系統殺死它的內存需求,系統照顧儘快重新創建)。服務的任務需要在服務的主線程的單獨線程中執行,以避免ANR。 下面是一個例子:

public class BackgroundService extends Service { 


    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 



    Thread t = new Thread(new Runnable() { 
     @Override 
     public void run() { 
     // Task to perform in the background - NOT in the MAIN thread (No UI changes) 

     } 
    }); 


    t.start(); 


    // We want this service to continue running until it is explicitly 
    // stopped, so return sticky. 
    return START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
    } 


} 
1

啓動後臺服務/應用開機後完成了ANDROID

在添加此行您的清單

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

<receiver android:enabled="true" android:name=".OnBootReceiver"> 

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

您的清單現在這個樣子加入後以上行 例如:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

<application 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" > 
<activity 
    android:name=".MainActivity" 
    android:label="@string/app_name" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 


<service android:name=".MyBackgroundService" 
      android:label="@string/app_name" 
      > 
</service> 

<receiver android:enabled="true" android:name=".OnBootReceiver"> 
<intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
</receiver> 
</application> 

如果你想開機完成後啓動服務,使用這個類

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 

public class OnBootReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
     if (prefs.getBoolean("AUTOSTART_SERVICE", false)) { 
       context.startService(new Intent(context, MyBackgroundService.class)); 
     } 
    } 
} 
} 

如果你想啓動應用程序本身的啓動完成後,使用這個類

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 

public class OnBootReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
     if (prefs.getBoolean("AUTOSTART_APPLICATION", false)) { 
      Intent mIntent = new Intent(); 
      mIntent.setClassName("com.example", "com.example.MainActivity"); 
      mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(mIntent); 
     } 
    } 
} 
} 

如果您有一個設置選項來啓用/禁用此啓動接收器,然後更改共享的值偏好如下,否則你可以從OnBootReceiver類

public void startAppOnBoot(Boolean start){ 
    SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(context); 
    SharedPreferences.Editor mEditor = mPreferences.edit(); 
    mEditor.putBoolean("AUTOSTART_APPLICATION", start); 
    mEditor.commit(); 
} 

public void startSerciceOnBoot(Boolean start){ 
    SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(context); 
    SharedPreferences.Editor mEditor= mPreferences.edit(); 
    mEditor.putBoolean("AUTOSTART_SERVICE", start); 
    mEditor.commit(); 
} 

刪除共享的喜好,如果你需要服務的一個例子中的android請點擊此鏈接: http://www.tutorialspoint.com/android/android_services.htm

相關問題