有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

API:10&;十八

IDE:Eclipse

测试设备:仿真器

我知道这个问题已经在stackoverflow和其他地方广泛讨论过。但我无法让它工作。我的第一个问题是

  • 是否可以通过编程方式解锁模拟器,并在启动时加载应用程序
  • 我还了解到,在API 13之后,出现了一些变化,我不确定我是否考虑了这些变化

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

AndroidManifest。xml

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


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

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

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

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

主要活动。java

package com.example.display;

import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.content.res.Configuration;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.view.Window;
import 安卓.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.安卓.com/reference/安卓/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

我希望这段代码在启动时执行并启动应用程序

package com.example.display;
import 安卓.content.BroadcastReceiver;
import 安卓.content.Context;
import 安卓.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);
    }

}

问题:我已将上述代码加载到仿真器并重新启动仿真器。我希望代码应用程序解锁模拟器并加载启动应用程序。这不会发生

不知道下一步该去哪里找

大多数代码段都来自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

先谢谢你


共 (1) 个答案

  1. # 1 楼答案

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

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