gpt4 book ai didi

android - 仅当我有一个打开的特定应用程序时,才可以在 Android 中执行服务吗?

转载 作者:行者123 更新时间:2023-11-30 00:55:23 24 4
gpt4 key购买 nike

有什么方法可以仅在特定应用程序打开时执行服务(启动或停止)?案例是我有一个“泡泡”服务(类似于 facebook),我只想在我打开 Minecraft PE 游戏时出现。当我更改或打开另一个应用程序时,我想放置不可见的气泡。这可能吗?

最佳答案

使用此代码。

public class CheckRunningApplicationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context aContext, Intent anIntent) {
try {
ActivityManager am = (ActivityManager) aContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> alltasks = am
.getRunningTasks(1);
for (ActivityManager.RunningTaskInfo aTask : alltasks) {

if (aTask.topActivity.getClassName().equals("com.android.phone.InCallScreen")
|| aTask.topActivity.getClassName().equals("com.android.contacts.DialtactsActivity"))
{
Toast.makeText(aContext, "Phone Call Screen.", Toast.LENGTH_LONG).show();
}
String packageName = "com.example.checkcurrentrunningapplication";

if (aTask.topActivity.getClassName().equals(
packageName + ".Main"))
{
// When opens this example screen then show a alert message
Toast.makeText(aContext, "Check Current Running Application Example Screen.",
Toast.LENGTH_LONG).show();
}
}

} catch (Throwable t) {
Log.i("THROW", "Throwable caught: "
+ t.getMessage(), t);
}
}
}

在 list 中添加:

<receiver android:name = ".CheckRunningApplicationReceiver"/>
<uses-permission android:name="android.permission.GET_TASKS" />

关于android - 仅当我有一个打开的特定应用程序时,才可以在 Android 中执行服务吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40258897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com