简体   繁体   English

后台服务无法正常运行

[英]Background service isn't working properly

I'm trying to implement service in android to make an app locker. 我正在尝试在android中实现服务,以使应用程序更衣柜。

I'm trying to check the which activity is running on the foreground and if it's locked, forwarding it to my Locker activity. 我正在尝试检查哪个活动正在前台运行,以及是否已锁定,请将其转发到我的Locker活动。

I've added the service in manifest too, but it isn't working at all. 我也已在清单中添加了该服务,但它根本无法正常工作。 Here's the code ` 这是代码

    private static Timer timer = new Timer();
    public Boolean userAuth = false;
    private Context mContext;
    public String pActivity = "";

    public IBinder onBind(Intent arg0) {
        return null;
    }

    public void onCreate() {
        super.onCreate();
        mContext = this;
        startService();
    }

    private void startService() {
        timer.scheduleAtFixedRate(new mainTask(), 0, 500);
    }

    private class mainTask extends TimerTask {
        public void run() {
            toastHandler.sendEmptyMessage(0);
        }
    }

    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
    }

    private final Handler toastHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String activityOnTop;

            ActivityManager manager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);

            List<ActivityManager.RunningAppProcessInfo> tasks = manager.getRunningAppProcesses();
            //Getting the foreground activity name
            activityOnTop=tasks.get(0).processName;
            //Checking it against the app I need to lock
            if (activityOnTop.equalsIgnoreCase("com.droiddigger.techmanik")) {
                Intent lockIntent = new Intent(mContext, Lockscreen.class);
                lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(lockIntent);
            } else if(activityOnTop.equalsIgnoreCase("com.droiddigger.applocker")){

            }else{

            }
        }
    };

You must start that service. 您必须启动该服务。 It can be done in an Activity or a BroadcastReceiver. 可以在Activity或BroadcastReceiver中完成。

startService(new Intent(this, UpdaterServiceManager.class));

For example: 例如:

public class MyActivity extends Activity {

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startService(new Intent(this, YourService.class));
        finish();
    }

}

EDIT: 编辑:

You are always retrieving the item 0 of the list called tasks . 您总是在检索名为task的列表的项目0。 Looking at the SDK documentation, it is said that list order is not especified: https://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses() 查看SDK文档时,据说未指定列表顺序: https : //developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses()

Returns a list of RunningAppProcessInfo records, or null if there are no running processes (it will not return an empty list). 返回RunningAppProcessInfo记录的列表,如果没有正在运行的进程,则返回null(它不会返回空列表)。 This list ordering is not specified. 未指定此列表顺序。

You must get the current visible activity other way. 您必须以其他方式获得当前可见的活动。 I suggest an AccessibilityService 我建议使用AccessibilityService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM