2015-04-30 16 views
2

我正在通過adb廣播自定義意圖「com.example.demo.action.LAUNCH」來開發一個需要啓動應用程序的項目。從ADB安裝後靜態BroadcastReceiver無法工作

我的計劃是靜態註冊一個廣播接收器「LaunchAppReceiver」,它將在接收到定製意圖時啓動應用程序。

我通過調用

adb install -r <pakcageName> 

安裝apk文件,然後我通過調用

adb shell am broadcast -a com.example.demo.action.LAUNCH 

但是發送的意圖,什麼都沒有發生的意圖發出後。看起來廣播接收機根本沒有收到意圖。我是否需要在收到意圖之前實例化接收者?

注:由於android設備是遠程的,我必須使用adb來處理安裝和啓動。

謝謝!


我宣佈廣播接收機如下

public class LaunchAppReceiver extends BroadcastReceiver{ 

    public LaunchAppReceiver() {} 

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

     Intent newIntent = new Intent(context, MainActivity.class); 
     newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(newIntent); 
    } 
} 

和靜態在AndroidManifest.xml註冊它。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.demo" > 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" 
     android:enabled="true"> 
     <receiver 
      android:name="com.example.demo.LaunchAppReceiver" 
      android:enabled="true" 
      android:exported="true"> 
      <intent-filter> 
       <action android:name="com.example.demo.action.LAUNCH"/> 
      </intent-filter> 
     </receiver> 
     <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> 
    </application> 
</manifest> 
+0

好像你需要運行應用程序至少一次在系統中註冊廣播。 – jauseg

+0

是的,它似乎是真的......現在的情況是,我打算從廣播接收器啓動應用程序,然而需要運行應用程序進行註冊...陷入循環。 – hackjutsu

+0

嗯...我認爲應該工作..你看到你的logcat上的任何錯誤? LaunchAppReceiver是否在正確的包中(com.example.demo.LaunchAppReceiver)? – Buddy

回答

7

終於搞定了。

由於Honeycomb,所有新安裝的應用程序都會進入STOP階段,直到它們啓動至少一次。 Android爲所有廣播意圖添加了一個標記「FLAG_EXCLUDE_STOPPED_PACKAGES」,以防止它們到達停止的應用程序。 http://droidyue.com/blog/2014/01/04/package-stop-state-since-android-3-dot-1/

要解決此問題,只需簡單地添加標誌「FLAG_INCLUDE_STOPPED_PACKAGES」到我們發送的意圖。在我的情況下,我修改adb命令爲

adb shell am broadcast -a com.example.demo.action.LAUNCH --include-stopped-packages