【发布时间】:2019-12-23 11:27:51
【问题描述】:
您好,我是 android 开发新手,我尝试搜索但没有得到答案,我想在服务中获取所有打开的应用程序包名称并使用我的数据库检查它们,我尝试了此代码但没有工作,我想要运行应用程序过去和未来的包名,所以我想用服务试试这个......
【问题讨论】:
标签: android service android-service
您好,我是 android 开发新手,我尝试搜索但没有得到答案,我想在服务中获取所有打开的应用程序包名称并使用我的数据库检查它们,我尝试了此代码但没有工作,我想要运行应用程序过去和未来的包名,所以我想用服务试试这个......
【问题讨论】:
标签: android service android-service
package com.rasamohtava.access;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
public class CheckAppLaunchThread extends Thread {
private Context context;
private Handler handler;
private ActivityManager actMan;
private int timer = 100;
public static final String TAG = "App Thread";
public static String lastUnlocked;
// private String lastUnlocked;
public CheckAppLaunchThread(Handler mainHandler, Context context) {
this.context = context;
this.handler = mainHandler;
actMan = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
this.setPriority(MAX_PRIORITY);
}
@Override
public void run() {
context.startService(new Intent(context, AppLockService.class));
Looper.prepare();
String prevTasks;
String recentTasks = "";
prevTasks = recentTasks;
Log.d("Thread", "Inside Thread");
while (true) {
try {
String topPackageName = "";
if(Build.VERSION.SDK_INT >= 21) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
// We get usage stats for the last 10 seconds
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*5, time);
if(stats != null) {
SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
}
if(mySortedMap != null && !mySortedMap.isEmpty()) {
topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
}
else {
topPackageName = actMan.getRunningAppProcesses().get(0).processName;
}
recentTasks = topPackageName;
Thread.sleep(timer);
if (recentTasks.length()==0 || recentTasks.equals(prevTasks)) {
}
else {
if (isAppLocked(recentTasks)) {
Log.d(TAG, "Locked miladk313" + recentTasks);
handler.post(new RequestPassword(context, recentTasks));
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
prevTasks = recentTasks;
}
}
class ToastRunnable implements Runnable {
String message;
public ToastRunnable(String text) {
message = text;
}
@Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
class RequestPassword implements Runnable {
private Context mContext;
private String pkgName;
public RequestPassword(Context mContext, String pkgName) {
this.mContext = mContext;
this.pkgName = pkgName;
}
@Override
public void run() {
Intent passwordAct = new Intent(context, BlockActivity.class);
passwordAct.putExtra("PACKAGE_NAME", pkgName);
passwordAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
this.mContext.startActivity(passwordAct);
}
}
private boolean isAppLocked(String packageName) {
if (packageName.equals(BlockActivity.lastUnlocked)) {
return false;
}
BlockActivity.lastUnlocked = null;
DatabaseManager dbHelper = new DatabaseManager(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM tbl_blacklist WHERE package_name=\'"
+ packageName + "\'", null);
boolean isLocked = false;
if (cursor.moveToNext()) {
isLocked = true;
}
cursor.close();
db.close();
dbHelper.close();
return isLocked;
}
}
【讨论】: