所以现在我正在尝试为我的 child 开发一个 Android 应用程序。我想在特定时间内对选定的应用程序设置 PIN 码或密码,以防止它们打开该应用程序。例如,假设我工作时,我女儿想在我的手机上玩《愤怒的小鸟》一段时间。当她玩《愤怒的小鸟》时,我会选择我的重要应用程序,如消息、gmail 等,并在其上设置 PIN 或密码 30 分钟。 30 分钟后,我从女儿那里拿到了手机,我可以在没有 PIN 码的情况下打开应用程序,因为时间限制已过。
我做了大量的研究,并且能够编写基本的服务类(class)。
package com.spicycurryman.getdisciplined10.app;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Created by Spicycurryman on 8/21/14.
*/
public class SaveMyAppsService extends Service{
public void onCreate(){
super.onCreate();
//UNABLE TO SETCONTENTVIEW HERE. METHOD DOESN'T WORK
while(true) {
Toast.makeText(SaveMyAppsService.this,
"Your Message", Toast.LENGTH_LONG).show();
}
}
String CURRENT_PACKAGE_NAME = "com.spicycurryman.getdisciplined10.app.dev";
String lastAppPN = "";
boolean noDelay = false;
public static SaveMyAppsService instance;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
scheduleMethod();
CURRENT_PACKAGE_NAME = getApplicationContext().getPackageName();
Log.e("Current PN", "" + CURRENT_PACKAGE_NAME);
instance = this;
return START_STICKY;
}
private void scheduleMethod() {
// TODO Auto-generated method stub
ScheduledExecutorService scheduler = Executors
.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// This method will check for the Running apps after every 100ms
if(29==30 ) //check if the time is spent
{
stop();
}
else{
checkRunningApps();
}
}
}, 0, 100, TimeUnit.MILLISECONDS);
}
public void checkRunningApps() {
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);
// Provide the packagename(s) of apps here, you want to show password activity
if (activityOnTop.contains("com.android.camera") // you can make this check even better
|| activityOnTop.contains(CURRENT_PACKAGE_NAME)) {
while(true) {
Toast.makeText(SaveMyAppsService.this,
"Your Message", Toast.LENGTH_LONG).show();
}
} else {
// DO nothing
}
}
public static void stop() {
if (instance != null) {
instance.stopSelf();
}
}
}
我使用这段代码来启动它。
startService(new Intent(this, SaveMyAppsService.class));
本质上,我只是想创建一个自定义锁定屏幕并将其显示给用户,以便他们必须输入正确的 PIN 或密码才能进入应用程序。但我在这里无法做到这一点。
我如何为我选择的应用程序设置密码或固定屏幕的内容 View (假设我有包名称),以便我可以使用我的服务类保护我的应用程序?
最佳答案
我认为部分问题可能是您尝试在服务中使用 setContentView(),但它需要一个 Activity/UI 来实际设置内容。所以你可能会做类似的事情
YourActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
// your setContentView code
}
});
编辑1:
Intent localIntent = new Intent("android.intent.action.VIEW");
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.setClassName("com.example.applockerservice", "com.example.applockerservice.AppLockerScreen");
startActivity(localIntent);
关于java - 如何在服务类中设置ContentView以在打开时保护应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25458319/