微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 按电源按钮启动应用程序

我发现这个code通过按下电源按钮启动应用程序.

而且我不知道为什么这段代码对我不起作用,但无论如何我要放一些带清单的代码,请检查……

并告诉我,通过单击或两次按下电源按钮启动应用程序是否正确?

   toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);

    btnToggleLock.setonClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (btnToggleLock.isChecked()) {    

                    toast.cancel();
                    toast.setText("Unlocked");
                    toast.show();

                    Log.i("Unlocked", "If");

                    Context context = getApplicationContext();
                    KeyguardManager _guard = (KeyguardManager) context
                            .getSystemService(Context.KEyguard_SERVICE);
                    KeyguardLock _keyguardLock = _guard
                            .newKeyguardLock("KeyguardLockWrapper");
                    _keyguardLock.disableKeyguard();

                    PanicButtonActivity.this.startService(new Intent(
                            PanicButtonActivity.this, UpdateService.class));

                } else {

                    toast.cancel();
                    toast.setText("Locked");
                    toast.show();

                    Context context = getApplicationContext();
                    KeyguardManager _guard = (KeyguardManager) context
                            .getSystemService(Context.KEyguard_SERVICE);
                    KeyguardLock _keyguardLock = _guard
                            .newKeyguardLock("KeyguardLockWrapper");
                    _keyguardLock.reenableKeyguard();

                    Log.i("Locked", "else");

                    PanicButtonActivity.this.stopService(new Intent(PanicButtonActivity.this,
                            UpdateService.class));

                }

            }
        });

Receiver.java:

public class Receiver extends broadcastReceiver {

    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}

服务:

public class UpdateService extends Service {

    broadcastReceiver mReceiver;

@Override
public void onCreate() {
    super.onCreate();
    // register receiver that handles screen on and screen off logic
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    mReceiver = new Receiver();
    registerReceiver(mReceiver, filter);
}

@Override
public void onDestroy() {

    unregisterReceiver(mReceiver);
    Log.i("onDestroy Reciever", "Called");

    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
    boolean screenOn = intent.getBooleanExtra("screen_state", false);
    if (!screenOn) {
        Log.i("screenON", "Called");
        Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG)
                .show();
    } else {
        Log.i("screenOFF", "Called");
    }
}

@Override
public IBinder onBind(Intent intent) {
    // Todo Auto-generated method stub
    return null;
}
}

的Manifest.xml: –

 <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

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

   <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.panic.app.PanicButtonActivity"
        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.panic.app.Receiver" />
    <service android:name="com.panic.app.UpdateService" />

</application>

解决方法:

首先,与其他广泛的铸造intentsso不同,您需要制作一个将继续运行的服务:

public static class UpdateService extends Service {

        @Override
        public void onCreate() {
            super.onCreate();
            // register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            broadcastReceiver mReceiver = new Receiver();
            registerReceiver(mReceiver, filter);
        }

        @Override
        public void onStart(Intent intent, int startId) {
            boolean screenOn = intent.getBooleanExtra("screen_state", false);
            if (!screenOn) {
                // your code
            } else {
                // your code
            }
        }
}

你的接收器可以是某种东西

public class Receiver extends broadcastReceiver {

    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐