使用Service Class阻止了我自己的应用程序

时间:2014-08-26 02:58:11

标签: java android android-intent android-service android-service-binding

我正在尝试锁定所选应用,然后再打开它们一段特定的时间。

经过大量研究和失败的实施。我终于让我的服务类在某些方面与我的LockScreen活动有关。最大的问题是它在过程中锁定了我的非常自己的应用程序。

这是我的服务类(为方便起见,我发表了评论,并帮助理解。相关部分是代码的末尾。):

package com.alimuzaffar.android.childlock;

import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

public class HeartBeat extends Service {



    private static final String TAG = HeartBeat.class.getSimpleName();
    public Timer TIMER;

   String CURRENT_PACKAGE_NAME;


    private static Set<AccessGranted> mAccessGrantedList = new HashSet<AccessGranted>();
    private Set<String> mLockedApps = new HashSet<String>();
    private long lastModified = 0;
    private BroadcastReceiver mScreenStateReceiver;
    private BroadcastReceiver mAccessGrantedReceiver;
    private File mLockedAppsFile;


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


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


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

        // Log.i("LocalService", "Received start id " + startId + ": " +
        // intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        if (TIMER == null) {

            TIMER = new Timer(true);
            TIMER.scheduleAtFixedRate(new LockAppsTimerTask(), 1000, 250);

            mScreenStateReceiver = new BroadcastReceiver() {

                private boolean screenOff;

                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                        screenOff = true;
                    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                        screenOff = false;
                    }

                    if (screenOff) {
                        Log.i(TAG, "Cancel Timer");
                        TIMER.cancel();
                    } else {
                        Log.i(TAG, "Restart Timer");
                        TIMER = new Timer(true);
                        TIMER.scheduleAtFixedRate(new LockAppsTimerTask(), 1000, 250);
                    }
                }
            };

            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            registerReceiver(mScreenStateReceiver, filter);

            mAccessGrantedReceiver = new BroadcastReceiver() {

                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    String packageName = intent.getStringExtra("packageName");
                    if (action.equals(Constants.ACTION_GRANT_ACCESS) && packageName != null) {
                        AccessGranted ag = new AccessGranted(packageName);
                        mAccessGrantedList.remove(ag);
                        mAccessGrantedList.add(ag);
                    }
                }
            };

            IntentFilter filter2 = new IntentFilter(Constants.ACTION_GRANT_ACCESS);
            registerReceiver(mAccessGrantedReceiver, filter2);
        }
        // this.stopSelf();



 //startforeground goes here



        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        startService(new Intent(this, HeartBeat.class));
    }


    private class LockAppsTimerTask extends TimerTask {

        @Override
        public void run() {
            ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);

            try {
                //List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(1, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
                String packageName = "com.android.camera";
                ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
                        .getRunningTasks(1);
                ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
                String activityOnTop = ar.topActivity.getPackageName();

               // Log.e("activity on Top", "" + activityOnTop);
             //   Log.e(" My package name", "" + getApplicationContext().getPackageName());



// Provide the packagename(s) of apps here, you want to show password activity




// THE PROBLEM IS OVER HERE. BUT I AM NOT SURE WHAT IS WRONG. I USED INSTAGRAM AS A TEST. BUT I AM NOT SURE WHY MY OWN PERSONAL APP IS GETTING BLOCK
                if (activityOnTop.contains("com.instagram.android")  
                       ) {

                    Intent i = new Intent(getApplicationContext(), LockScreenActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    i.putExtra("packageName", packageName);
                    startActivity(i);
                }


            } catch (Exception e) {
                Log.e("Foreground App", e.getMessage(), e);
            }
        }



    }

}

如果你需要它,我的锁屏活动是:

package com.alimuzaffar.android.childlock;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.spicycurryman.getdisciplined10.app.R;

public class LockScreenActivity extends Activity {
    private static final String TAG = LockScreenActivity.class.getSimpleName();
    EditText pin;
    TextView pinMsg;
    String firstPin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);

    }

    @Override
    public void onBackPressed() {
        // Display confirmation here, finish() activity.
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);

        Intent i = new Intent(LockScreenActivity.this, HeartBeat.class);
        startService(i);

        finish();
        super.onBackPressed();
    }

}

在我的服务类中,我不确定if语句有什么问题。它应该只适用于Instagram,因为这是检查的内容,但我的应用程序也被阻止...

我该如何解决这个问题?我需要对代码进行哪些修改?

0 个答案:

没有答案