【问题标题】:Start a service automatically when app installed to device [duplicate]将应用程序安装到设备时自动启动服务[重复]
【发布时间】:2014-12-23 15:07:05
【问题描述】:

我想在安装应用时自动启动服务。启动后或设备启动后服务正常工作。那么,是否可以在安装应用程序后启动服务?

【问题讨论】:

    标签: android android-service android-broadcast


    【解决方案1】:

    Google 将正确答案显示为第一次点击所以...您对此进行了一些研究吗? How to start a Service when .apk is Installed for the first time

    总结:你不能这样做。

    【讨论】:

    • 我做了研究。所有的帖子和 cmets 都是旧的,我在想最新版本中的任何更新都可以做到这一点。不过,感谢您的总结。
    【解决方案2】:

    从技术上讲,安装应用时无法启动服务。可以从 Google Glass Development Kit 开始。可以选择通过 Voice 安装您的应用,也可以启动服务(Voice Trigger 命令)。

     <service
                android:name="com.est.poc.glass.service.POCGlassService"
                android:enabled="true"
                android:exported="true"
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
                </intent-filter>
                <!-- Voice command found in res/xml/voice_trigger_start -->
                <meta-data
                    android:name="com.google.android.glass.VoiceTrigger"
                    android:resource="@xml/voice_trigger_start" />
                <meta-data
                    android:name="com.google.android.glass.voice_trigger"
                    android:resource="@string/voice_trigger_title" />
            </service>
    

    【讨论】:

      【解决方案3】:

      是的,可以通过收听已安装包的广播来实现

      这是你的广播

      public class InstallBroadcastReceiver extends BroadcastReceiver {
      
          @Override
          public void onReceive(Context context, Intent intent) {
              String action = null;
              if (intent != null) {
                  action = intent.getAction();
              }
      
              if (action != null && Intent.ACTION_PACKAGE_ADDED.equals(action)) {
                  String dataString = intent.getDataString();
                  if (dataString != null
                          && dataString.equals(YOUR_PACKAGE_NAME)) {
      
                      //Launch your service :)
                  }
              }
          }
      }
      

      这是你的清单

      <receiver
              android:name=".InstallBroadcastReceiver"
              android:enabled="false"
              android:exported="false">
          <intent-filter>
              <action android:name="android.intent.action.PACKAGE_ADDED"/>
              <data android:scheme="package"/>
          </intent-filter>
      </receiver>
      

      希望对你有所帮助;)

      【讨论】:

      • 你真的试过这个吗?如果您的应用收到您的应用已安装的广播,我会感到惊讶。我很确定这只会广播给其他已安装的应用程序。编辑:来自文档:广播操作:设备上已安装了一个新的应用程序包。数据包含包的名称。注意新安装的包没有收到这个广播。
      • 它确实收到了我测试过的并且它工作正常,在用户接受安装对话框后它会遇到这个广播。看看developer.android.com/reference/android/content/Intent.html在你投下一个问题之前试试代码...
      • 我根据文档(声明它不受支持)和其他用户在重复答案中明确表示它不起作用的陈述投了反对票。也许它可以在某些变体中起作用,但它并不打算起作用。
      • 奇怪的是它在我的情况下的工作,我会进一步调查,我会在之后回复你。感谢您的诚实回答;)
      • 我看到它会产生竞争条件。应用正在等待自己在安装时收到通知。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      相关资源
      最近更新 更多