【问题标题】:launch/cancel activity immediatly when conditions are met in a service在服务中满足条件时立即启动/取消活动
【发布时间】:2014-11-15 16:16:31
【问题描述】:

所以我正在为 android 构建一个应用程序储物柜。我已经为我的储物柜活动编写了一个服务,一旦在 android 应用程序堆栈的顶部检测到特定活动,就会启动它。要锁定的应用程序列表存储在数据库中的一个表。在大多数情况下,我的服务工作正常。但我面临一些我不知道如何克服的问题。任何帮助都会被广泛接受

我想解决以下问题。

1) 当我按下后退按钮时,我希望我的储物柜活动和被锁定的应用程序停止执行。就我而言,当我按下后退按钮时,lockeractivity 会一次又一次地启动。

2) 当需要锁定的特定应用程序启动时,该应用程序会在屏幕上显示 1 秒或 2 秒,然后调用我的储物柜活动。我希望直接调用我的储物柜活动。

3) 有时,一旦用户已经验证了自己不想要的,我的储物柜活动就会再次调用。 (我认为这是服务检查前台活动时间的原因)

我的服务

public class LockerService extends Service {
private static final String TAG = "MyService";
String LockedApps[];
String allowedapp = null;
DataBaseHandler handler;

private final static Handler servicehandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {

    }
};

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    handler = new DataBaseHandler(this);

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

private Runnable checkforeground = new Runnable() {
    public void run() {
        // / Any thing you want to do put the code here like web service
        // procees it will run in ever 1 second
        handler.open();
        LockedApps = handler.getPackages();
        handler.close();

        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am
                .getRunningAppProcesses();
        String foregroundapp = runningAppProcessInfo.get(0).processName
                .toString();
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(LockerService.this);
        allowedapp = sp.getString("allowedapp", "anon");

        if ((Arrays.asList(LockedApps).contains(foregroundapp))
                && (allowedapp.equals(foregroundapp))) {
            // do nothing
        } else if (Arrays.asList(LockedApps).contains(foregroundapp)) {
            // show your activity here on top of
            // PACKAGE_NAME.ACTIVITY_NAME
            Intent lockIntent = new Intent(getBaseContext(), Locker.class);
            lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            getBaseContext().startActivity(lockIntent);
        }
        servicehandler.postDelayed(this, 1500); // 1.5 seconds
    }
};

@Override
public void onStart(Intent intent, int startId) {

    servicehandler.removeCallbacks(checkforeground);
    servicehandler.postDelayed(checkforeground, 1500);// 1.5 second
    Log.d(TAG, "onStart");
}

}

【问题讨论】:

    标签: java android security service


    【解决方案1】:

    只需在储物柜活动中覆盖 onBackPressed() 方法并向主启动器触发意图。这没什么,但它会将您带到主屏幕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 2017-03-12
      相关资源
      最近更新 更多