Sae*_*abi 6 android broadcastreceiver lockscreen
我为adnroid设计了一个锁屏.当用户按下电源按钮解锁时,我正在尝试使用广播接收器来启动锁屏活动.
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
Log.w("BAM", "Screen went on");
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
Log.w("BAM","Screen went off");
}
}
Run Code Online (Sandbox Code Playgroud)
}
android清单:`
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyLockScreenActivity"
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="MyAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
<receiver android:name=".Receiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
<activity android:name=".LockScreen"></activity>
</application>
Run Code Online (Sandbox Code Playgroud)
`
private ActionBar actionBar;
private ViewPager viewer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.lockscreen);
actionBar = getSupportActionBar();
actionBar.hide();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new Receiver();
registerReceiver(mReceiver, filter);
viewer = (ViewPager) findViewById(R.id.viewr);
viewer.setAdapter(new MyAdapter(getSupportFragmentManager()));
}
Run Code Online (Sandbox Code Playgroud)
但是当用户按下电源按钮时,锁定屏活动在1-3秒后开始.当用户先按下电源按钮屏幕关闭时启动活动是否合适?我该怎么做?
感谢您的意见.(对不起,我的英语不好!)
\n\n\n首先,与其他广泛传播的意图不同,对于 Intent.ACTION_SCREEN_OFF 和 Intent.ACTION_SCREEN_ON,您不能在 Android 清单中声明它们!I\xe2\x80\x99m 不确定具体原因,但它们必须在 JAVA 代码中的 IntentFilter 中注册
\n
使用:
\n\n再次编辑
\n\npublic class YourActivity extends Activity {\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.YourLayout);\n\n IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);\n filter.addAction(Intent.ACTION_SCREEN_OFF);\n BroadcastReceiver mReceiver = new ScreenReceiver();\n registerReceiver(mReceiver, filter);\n }\n\n public class ScreenReceiver extends BroadcastReceiver{\n @Override\n public void onReceive(Context context, Intent intent) {\n\n if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){\n Log.w("BAM", "Screen went on");\n }\n\n else if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){\n Log.w("BAM","Screen went off");\n }\n }\n }\n} \nRun Code Online (Sandbox Code Playgroud)\n\n已测试,有效!
\n| 归档时间: |
|
| 查看次数: |
1147 次 |
| 最近记录: |