/**
* Created by lhy on 2018/1/11.
* 1.定义一个类继承service 在dialog show方法之前添加如下代码,表示该dialog是一个系统的dialog
dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
* 2..在Manifest.xml文件中配置该Service;并且申请权限android.permission.SYSTEM_ALERT_WINDOW
*/
public class MyService extends Service {
private static final String TAG = "ExampleService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "MyService-onBind");
return null;
}
@Override
public void onCreate() {
Log.i(TAG, "MyService-onCreate");
super.onCreate();
}
/**
* @param intent 方法过时了
* @param startId
*/
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "MyService-onStart");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage("Hello,服务中开启弹窗.");
builder.setNegativeButton("CANCEL", null);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 具体操作
}
});
final Dialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i(TAG, "MyService-onDestroy");
super.onDestroy();
}
}