2015-12-29 32 views
0

我想在android中創建一個應用程序,安裝後我需要在設備打開時設置爲默認應用程序。如何創建一個Android應用程序,該應用程序在打開設備時設置爲默認值

我曾嘗試以下

創建服務, 創建reciever到收到啓動完成事件

它不工作actually.If移除啓動了MainActivity的,它會顯示錯誤說

No Launcher activity found! 
The launch will only sync the application package on the device! 

我的清單文件

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:screenOrientation="sensorLandscape" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service 
      android:name=".MyService" 
      android:label="My Service" > 
      <intent-filter> 
       <action android:name="com.example.splash_test.MyService" /> 
      </intent-filter> 
     </service> 

     <receiver 
      android:name=".receiver.MyReceiver" 
      android:label="MyReceiver" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 


      </intent-filter> 
     </receiver> 

     <activity 
      android:name="com.example.splash_test.Screen1" 
      android:label="@string/app_name" 
      android:screenOrientation="sensorLandscape" > 
      </activity> 
    </application> 

MyReceiver.java

public class MyReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { 
      Intent serviceIntent = new Intent(context, MyService.class); 
      context.startService(serviceIntent); 
     } 
    } 

} 

MyService.java

public class MyService extends Service { 

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

     if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 

      Intent i = new Intent(this, MainActivity.class); 
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      this.startActivity(i); 
     } 
     return null; 
    } 

} 

回答

2

你必須遵循以下步驟完成此任務: -

  1. 在你的應用程序撥打服務(運行所有的時間)。
  2. 瞭解您的應用在服務運行與否。(通過包名)
  3. 如果運行與服務的幫助下再優秀的其他明智的運行。

試試....它在我的應用程序中工作正常。 :)

+1

感謝Vishnu它的工作.. –

+0

@Vishnu:請參閱編輯... –

+0

@詹姆斯: - http://stackoverflow.com/questions/15085897/check-if-my-app-is-running -in-機器人 – Vishnu

相關問題