有没有办法仅在特定应用程序打开时执行服务(启动或停止)?情况是我有一个“泡泡”服务(类似于 facebook),我只想在我打开游戏 Minecraft PE 时出现。当我更改或打开另一个应用程序时,我想放置隐形气泡。这可能吗?
问问题
50 次
1 回答
1
使用此代码。
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);
}
}
}
在清单中添加:
<receiver android:name = ".CheckRunningApplicationReceiver"/>
<uses-permission android:name="android.permission.GET_TASKS" />
于 2016-10-26T10:15:22.173 回答