1.添加权限
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
2.添加界面PopActivity
在onCreate方法中添加以下代码
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED //锁屏显示
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD //解锁
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕不息屏
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);//点亮屏幕
setContentView(R.layout.activity_pop);
//下面就是根据自己的跟你需求来写,跟写一个Activity一样的
//拿到传过来的数据
String msg = getIntent().getStringExtra("msg");
textview = (TextView) findViewById(R.id.textview);
textview.setText("收到消息:" + msg);
3.布局代码activity_pop
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_pop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
tools:context="mwteck.ettda.pages.demojpush.PopActivity">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_centerInParent="true"
android:background="@color/gray_99_alpha"
android:textSize="14sp"
android:padding="10dp"
android:textColor="@color/white"
/>
</RelativeLayout>
4.在收到通知后调用以下方法
//打开弹窗
private void openPopWindow(Context context, String msg) {
//启动Activity
Intent alarmIntent = new Intent(context, PopActivity.class);
//携带数据
alarmIntent.putExtra("msg", msg);
//activity需要新的任务栈
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(alarmIntent);
}
5.演示效果【允许权限“锁屏显示”,“后台弹出界面”】