不需要权限
讲解部分:
http://blog.csdn.net/working_harder/article/details/52629919
代码实现
Mainnifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ruulai.tools">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:theme="@style/MainActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
<!-- 我们无法监听Home按键, 但是可以改变因Home进入后台时的处理, 比如在Manifest的activity声明中加上android:noHistory="true"
这样如果用户通过Home按键让我们的应用进入后台, 我们会让这个activity销毁, 就像我们被滑动关闭一样.
如果不加, 最好重写Activity的onNewIntent来应对 因Home 进入后台, 然后Service再次启动 锁屏Activity 的情况. -->
<activity android:excludeFromRecents="true"
android:noHistory="true"
android:exported="false"
android:launchMode="singleInstance"
android:name=".LockScreenActivity"
android:screenOrientation="portrait"
android:taskAffinity="com.ruulai.tools.LockScreenActivity"
android:theme="@style/MyTheme">
</activity>
<!--
上面的属性中android:excludeFromRecents="true"让锁屏Activity不显示在近期任务中,
android:launchMode="singleInstance"和android:taskAffinity="com.package.name.lockscreen"保证锁屏Activity有一个单独的Task, 且这个Task里永远只有它一个实例.
-->
</application>
</manifest>
主题 style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MainActivityTheme" parent="AppTheme">
<!--我们的锁屏Activity在滑动”解锁”之后, 理论上是直接进入下面的界面, 但有时如果下面不是launcher, 而是一个app, 有可能会闪一下黑屏, 这个其实是底下activity的入场动画导致的, 某些Android版本会对顶部activity透明时处理有些奇怪, 我们不能保证其他的应用不闪黑屏, 但是对自己的的应用还是可以的, 只需要在我们的主体activity的style中加上 -->
<item name="android:windowAnimationStyle">@null</item>
</style>
<style name="MyTheme" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>
Service
package com.ruulai.tools;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private BroadcastReceiver broadcastReceiver;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(MyService.this, "收到锁屏消息", Toast.LENGTH_SHORT).show();
if (action.equals(Intent.ACTION_SCREEN_OFF)) {
Intent lockscreen = new Intent(MyService.this, LockScreenActivity.class);
lockscreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // ------->
startActivity(lockscreen);
Toast.makeText(MyService.this, "启动锁屏Activity", Toast.LENGTH_SHORT).show();
}
}
};
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(broadcastReceiver, intentFilter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "Service Start");
return START_NOT_STICKY;
}
}
锁屏Activity
package com.ruulai.tools;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
public class LockScreenActivity extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
setContentView(R.layout.lock);
}
@Override
public void onBackPressed() {
// 锁屏界面当然不响应Back按键, 只需要重写Activity的onBackPressed方法即可
}
public void test(View view) {
Toast.makeText(this, "你点击了Test", Toast.LENGTH_SHORT).show();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
}
MainActivity
package com.ruulai.tools;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void open(View view) {
Intent intent = new Intent();
intent.setClass(this, MyService.class);
startService(intent);
}
}