java - 当服务满足条件时立即启动/取消 Activity

标签 java android security service

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

我想解决以下问题。

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

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

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

我的服务

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");
}

}

最佳答案

只需重写储物柜 Activity 中的 onBackPressed() 方法并向主启动器激发一个 Intent 。这没什么,但它会将您带到主屏幕。

关于java - 当服务满足条件时立即启动/取消 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26947863/

相关文章:

java - IntelliJ 中可能发生的事情在 Eclipse 中不可能发生?

android - 如何在点击期间在 fragment 之间切换?

java - 如何从 Spring 中的 Soap 消息中提取附件

java - Android ListView - 奇怪的回收行为

android - Android 上的 DH key 对生成时间

security - 拒绝文件访问但在 IIS 中服务器正常

security - 如何在 Haskell 中对字符串的 SHA256 哈希进行 Base64 UrlEncode?

Hive 插件的 Apache Ranger 多策略存储库

java - POST 请求发送 JSON 数据 Java HttpUrlConnection

java - 从 'a' || 简化 s.charAt 'b' || 'c' ||到一个 s.charAt