以编程方式解锁Android设备并在启动时加载应用程序

以编程方式解锁Android设备并在启动时加载应用程序

本文介绍了以编程方式解锁Android设备并在启动时加载应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

目标:以编程方式解锁Android设备并在启动时加载应用

Objective: Unlock Android device programmatically and load application on boot up

API :10& 18

API: 10 & 18

IDE :Eclipse

测试设备 :模拟器

我理解这个问题已在stackoverflow和其他地方广泛讨论过。但是我无法让它发挥作用。我的第一个问题

I understand this issue has been widely discussed on stackoverflow and elsewhere. But I am unable to get this to work. My first question is


  • 仿真器是否可以在程序上解锁并在启动时加载应用程序?

  • 我还读到,在API 13之后有一些变化,我不确定我是否考虑到这些变化

假设答案是肯定的,请查找下面的代码。

Assuming the answer is yes please find code excepts below.

AndroidManifest.xml

<manifest
package="com.example.display">


<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.display.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name="com.example.display.myreceiver"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

MainActivity.java

package com.example.display;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;

public class MainActivity extends Activity {

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

        // Unlock
        // http://developer.android.com/reference/android/app/Activity.html#getWindow()
        Window window = getWindow();

        window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

myreceiver.java

我希望代码的这一部分在启动时执行并启动应用程序。

I am expecting this section of the code to get executed on boot up and start the application.

package com.example.display;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class myreceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent myIntent = new Intent(context, MainActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startService(myIntent);
    }

}

问题:我已将上述代码加载到模拟器并重新启动模拟器。我期待代码应用程序解锁模拟器并加载启动应用程序。它不会发生...

Issue: I have loaded the above code to the emulator and re-started the emulator. I was expecting the code application to unlock the emulator and load the application of boot up. It doesn't happen...

不确定下一步要去哪里...

Not sure where to look for next...

大多数代码片段来自stackoverflow。

Most of the code snippets are from on stackoverflow.

我引用的一些帖子是





  1. Trying to start a service on boot on Android
  2. How to launch the application upon booting up the device?
  3. Android - Wake Up and Unlock Device

提前感谢您。

推荐答案

您好我在这里以编程方式添加解锁并使用以下代码启动我们的应用程序。您需要在广播接收器中添加解锁代码。
请尝试让我。谢谢

Hi here i added the unlock programmatically and launch our application using the below code.You need to add the unlock code in broadcast receiver.Please try and let me. Thanks

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

public class myreceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

     // Unlock the screen
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
            | PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE, "INFO");
    wl.acquire();

    KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardLock kl = km.newKeyguardLock("name");
    kl.disableKeyguard();

    Intent myIntent = new Intent(context, MainActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startService(myIntent);
}
}

这篇关于以编程方式解锁Android设备并在启动时加载应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:17