所以现在我正在为我的孩子们开发一款Android应用程序。我想在特定时间内在选定的应用程序上设置一个或多个密码,以防止他们打开应用程序。例如,让我们说我的女儿在我工作的时候想要在我的手机上玩愤怒的小鸟一段时间。我将选择我的重要应用程序,如消息,gmail等,并在她玩愤怒的小鸟时将针或密码放在上面30分钟。 30分钟后,我从女儿那里拿到手机,因为时间限制到期,我可以不用别针打开应用程序。
我已经对此做了大量研究,但我还没能找到针对我的具体案例的实施方案。
How do android "app lock" applications work?
我知道app锁具有与我想要做的类似的结构。我只想给锁定一个时间限制。
https://play.google.com/store/apps/details?id=com.domobile.applock&hl=en
我远离使用ActivityManager等杀死活动/应用程序。我真的只想在特定时间内在所选应用程序上使用干净的锁定屏幕。
我有一个CountdownTimer可以在我设置的时间内倒计时。如果我拥有所有的包名,我将如何修改此代码以在选定的时间内阻止某些应用程序?
start_timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder( MainActivity.this )
.setMessage( "Are you sure you want to block the selected apps for the set amount of time?" )
.setPositiveButton( "Yeah man!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d( "AlertDialog", "Positive" );
hourint = Integer.valueOf(number_text.getText().toString());
minuteint = Integer.valueOf(minute_text.getText().toString());
secondint = Integer.valueOf(second_text.getText().toString());
Log.i("YourActivity", "Hours: " + hourint);
Log.i("YourActivity", "Minutes: " + minuteint);
Log.i("YourActivity", "Seconds: " + secondint);
totalTimeCountInMilliseconds = ((hourint*60*60) +(minuteint*60) + (secondint)) * 1000; // time count
timeBlinkInMilliseconds = 30*1000;
countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
// 500 means, onTick function will be called at every 500 milliseconds
@Override
public void onTick(long leftTimeInMilliseconds) {
Context context = MainActivity.this;
long seconds = leftTimeInMilliseconds / 1000;
mSeekArc.setVisibility(View.INVISIBLE);
start_timer.setVisibility(View.INVISIBLE);
block_button1.setVisibility(View.INVISIBLE);
if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
// textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
// change the style of the textview .. giving a red alert style
if ( blink ) {
number_text.setVisibility(View.VISIBLE);
minute_text.setVisibility(View.VISIBLE);
second_text.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
number_text.setVisibility(View.INVISIBLE);
minute_text.setVisibility(View.INVISIBLE);
second_text.setVisibility(View.INVISIBLE);
}
blink = !blink; // toggle the value of blink
}
second_text.setText(String.format("%02d", seconds % 60));
minute_text.setText(String.format("%02d", (seconds / 60) % 60));
number_text.setText(String.format("%02d", seconds / 3600)); // format the textview to show the easily readable format
}
@Override
public void onFinish() {
// this function will be called when the timecount is finished
//textViewShowTime.setText("Time up!");
number_text.setVisibility(View.VISIBLE);
minute_text.setVisibility(View.VISIBLE);
second_text.setVisibility(View.VISIBLE);
mSeekArc.setVisibility(View.VISIBLE);
start_timer.setVisibility(View.VISIBLE);
block_button1.setVisibility(View.VISIBLE);
}
}.start();
}
})
.setNegativeButton("Nope!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog", "Negative");
dialog.cancel();
}
})
.show();
答案 0 :(得分:14)
<强>逻辑强>
现在代码示例:
要启动服务,请执行以下代码
startService(new Intent(this, SaveMyAppsService.class));
现在,在您的服务中,检查这样的包,
public class SaveMyAppsService extends android.app.Service
{
String CURRENT_PACKAGE_NAME = {your this app packagename};
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(30 minutes 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("whatsapp") // you can make this check even better
|| activityOnTop.contains(CURRENT_PACKAGE_NAME)) {
// Show Password Activity
} else {
// DO nothing
}
}
public static void stop() {
if (instance != null) {
instance.stopSelf();
}
}
}
答案 1 :(得分:2)
String lastAppPN = "";
public void checkRunningApps() {
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
String activityOnTop;
if (Build.VERSION.SDK_INT > 20) {
activityOnTop = mActivityManager.getRunningAppProcesses().get(0).processName;
} else {
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
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("whatsapp") // you can make this check even better
|| activityOnTop.contains(CURRENT_PACKAGE_NAME)) {
if (!(lastAppPN.equals(activityOnTop))) {
lastAppPN = activityOnTop;
Log.e("Whatsapp", "started");
}
} else {
if (lastAppPN.contains("whatsapp")) {
if (!(activityOnTop.equals(lastAppPN))) {
Log.e("Whatsapp", "stoped");
lastAppPN = "";
}
}
// DO nothing
}
}
答案 2 :(得分:0)
我创建了一个小型演示项目。 希望这对某人有用 Link to project
答案 3 :(得分:0)
您也可以使用 DialogAlertView:
我认为这很容易做到。没有人也可以破解密码。假设如果有人卸载(或清除数据)应用程序,那么他们必须重新登录或注册(这样更安全)。
提示 - 您可以将密码保存在云端或任何您想要的地方。