如何告诉应用程序它是从设备启动时启动的?

发布于 2024-12-27 14:12:05 字数 144 浏览 1 评论 0原文

您好,我正在为 Android 编写一个从启动时启动的应用程序,我想知道是否有一种方法可以告诉应用程序它是从设备启动时启动的?如果应用程序是手动启动的(即不是在设备启动时),我需要它做一些不同的事情。我正在使用 BroadcastReceiver 在设备启动时启动应用程序。

Hi I'm writing a application for android that is started from boot up and i wondered if there was a way of telling the application it was started from the boot up of the device? i need it to do something different if the application was manual started (i.e not when the device was started). i am using a BroadcastReceiver to start the application when the device starts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坚持沉默 2025-01-03 14:12:05

您可以制作两个不同的广播接收器,其中一个具有 ACTION_BOOT_COMPLETED用于意图过滤器,另一个具有您将使用的其他意图过滤器。

或者创建一个具有两个 Intent 过滤器的广播接收器,例如:

<receiver android:name=".BatteryReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>                               
        </intent-filter> 
        <intent-filter>
            <action android:name="SOMETHING_ELSE"/>                               
        </intent-filter>         
    </receiver>

然后在 onReceiver 中执行:

if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
  // do code for phone just powered on
} else {
  // do code for phone is already on
}

编辑
上面假设您在两种情况下使用 BroadcastReceiver ,但从您的问题来看可能并非如此。

因此,如果您要启动一个 Activity (或服务),那么在 BroadcastReceiver 代码中,您可以执行以下操作:

Intent i = new Intent(context, MyClass.class);
i.putExtra("STARTED_FROM_BOOT", true);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

然后在该活动中,您可以执行以下操作:

if (getIntent().hasExtra("STARTED_FROM_BOOT")){
   // do your code for when started from boot.
}

如果我需要添加什么。

You could either make two different broadcast receivers one that has ACTION_BOOT_COMPLETED for the intent filter, and another that has the other intent filter that you would use.

Or create one broadcastreceiver that has two intent filters like:

<receiver android:name=".BatteryReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>                               
        </intent-filter> 
        <intent-filter>
            <action android:name="SOMETHING_ELSE"/>                               
        </intent-filter>         
    </receiver>

and then in the onReceiver do:

if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
  // do code for phone just powered on
} else {
  // do code for phone is already on
}

EDIT:
The above assumes that you use the BroadcastReceiver under two circumstances, which may not be the case judging from your question.

So if you are starting an Activity (or service), then in the BroadcastReceiver code, you could do:

Intent i = new Intent(context, MyClass.class);
i.putExtra("STARTED_FROM_BOOT", true);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Then in the activity, you could do:

if (getIntent().hasExtra("STARTED_FROM_BOOT")){
   // do your code for when started from boot.
}

Let me know if I need to add anything.

雨落□心尘 2025-01-03 14:12:05

是的,在清单中将 broadcastreceiver 与 on bootcompleted 意图挂钩,当设备启动时,该接收器将被触发,您可以在那里做任何您想做的事情

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

yes, hook a broadcastreceiver with a on boot completed intent in the manifest and when the device boots up that receiver will be fired and you can do whatever you want there

<receiver android:name=".BatteryReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>                               
        </intent-filter>         
    </receiver>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文