1.权限申请 一.配置文件 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 二.申请方式 if (!Settings.canDrawOverlays(getApplicationContext())) { //启动Activity让用户授权 Intent intent = new Intent(); intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); intent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 11011); return; }
2.弹窗对象基本对象 AlertDialog
demo
package com.newys_asyl.ys_doctor.dialog; import android.Manifest; import android.app.AlertDialog; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.text.TextUtils; import android.view.View; import android.view.WindowManager; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.RotateAnimation; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.amap.api.maps2d.AMap; import com.amap.api.maps2d.CameraUpdateFactory; import com.amap.api.maps2d.MapView; import com.amap.api.maps2d.model.BitmapDescriptorFactory; import com.amap.api.maps2d.model.LatLng; import com.amap.api.maps2d.model.LatLngBounds; import com.amap.api.maps2d.model.Marker; import com.amap.api.maps2d.model.MarkerOptions; import com.example.mylibrary.webservice.OnWebService; import com.example.mylibrary.webservice.WebServiceNewUtils; import com.newys_asyl.ys_doctor.R; import com.newys_asyl.ys_doctor.entity.SmartHand; import java.math.BigDecimal; import java.util.Map; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; /** * Autor:Administrator * CreatedTime:2019/10/23 0023 * UpdateTime:2019/10/23 0023 14:54 * Des:自定义推送内容提示 * UpdateContent: * xyfx:评估措施 **/ public class PushCuffDialog extends AlertDialog implements OnWebService { @BindView(R.id.title) TextView title; @BindView(R.id.l_blood) TextView lBlood; @BindView(R.id.h_blood) TextView hBlood; // @BindView(R.id.content) //// EditText content; @BindView(R.id.comit) Button comit; @BindView(R.id.name) TextView name; @BindView(R.id.rate) TextView rate; @BindView(R.id.moudle_adv) TextView moudleAdv; // @BindView(R.id.copy) // Button copy; @BindView(R.id.phone) Button phone; @BindView(R.id.address_map) MapView addressMap; private SmartHand data; private Context context; private LatLng latlng; private AMap aMap; public PushCuffDialog(@NonNull Context context, SmartHand data) { super(context, R.style.Theme_AppCompat_Light_Dialog_Alert); this.context = context; this.data = data; } public PushCuffDialog(@NonNull Context context, int themeResId) { super(context, themeResId); } private Handler handler = new Handler() { @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: Toast.makeText(context.getApplicationContext(), "失败", Toast.LENGTH_LONG).show(); PushCuffDialog.this.dismiss(); break; case 1: Toast.makeText(context.getApplicationContext(), "成功", Toast.LENGTH_LONG).show(); PushCuffDialog.this.dismiss(); break; case 2: Toast.makeText(context.getApplicationContext(), "请输入医学指导", Toast.LENGTH_LONG).show(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.push_hand_dialog); ButterKnife.bind(this); this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); addressMap.onCreate(savedInstanceState); double w = new BigDecimal(data.getLatitude()).doubleValue(); double l = new BigDecimal(data.getLongitude()).doubleValue(); latlng = new LatLng(w, l); if (null != data) { name.setText("姓名:" + "张三"); lBlood.setText(null == data.gethBlood() ? "收缩压:" + "未知" : "收缩压:" + data.gethBlood()); hBlood.setText(null == data.getlBlood() ? "舒张压:" + "未知" : "舒张压:" + data.getlBlood()); rate.setText(null == data.getRate() ? "心率:" + "未知" : "心率:" + data.getRate()); title.setText("紧急呼救"); } init(); } /** * 初始化AMap对象 */ private void init() { if (aMap == null) { aMap = addressMap.getMap(); aMap.clear(); addMarkersToMap();// 往地图上添加marker } } /** * 在地图上添加marker */ private void addMarkersToMap() { MarkerOptions markerOption = new MarkerOptions().position(latlng).icon(BitmapDescriptorFactory .fromResource(R.mipmap.purple_pin)) .title("紧急救治") .snippet("老人紧急救助") .draggable(true); aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15)); Marker mark = aMap.addMarker(markerOption); mark.showInfoWindow(); } /** * 往地图上添加一个groundoverlay覆盖物 */ private void addOverlayToMap() { aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39.936713, 116.386475), 18));// 设置当前地图显示为北京市恭王府 LatLngBounds bounds = new LatLngBounds.Builder() .include(new LatLng(39.935029, 116.384377)) .include(new LatLng(39.939577, 116.388331)).build(); } @Override public void fail(Object response, int code, Exception e) { handler.sendEmptyMessage(0); } @Override public void success(Object response, int code, String tag) { if (null != response && response instanceof String) { if (TextUtils.equals("error", response.toString())) { handler.sendEmptyMessage(0); } else { handler.sendEmptyMessage(1); } } } @Override public void inProgress(float progress, long total, int id) { handler.sendEmptyMessage(0); } /** * Autor:Administrator * CreatedTime:2019/10/23 0023 * UpdateTime:2019/10/23 0023 19:58 * Des: * UpdateContent: **/ private void reqComit() { Map<String, Object> pram = WebServiceNewUtils.getInstance().initPram(); // if (TextUtils.isEmpty(content.getText().toString())) { // handler.sendEmptyMessage(0); // return; // } /* pram.put("id", data.getId()); pram.put("xyzd", content.getText().toString()); pram.put("xm", data.getXm()); pram.put("sfz", data.getSfz()); pram.put("tjrq", data.getTjrq()); pram.put("gy", data.getGy()); pram.put("dy", data.getDy()); pram.put("xyfx", data.getXyfx()); pram.put("ml", data.getMl());*/ WebServiceNewUtils.getInstance().post().webServerUrl("http://www.adaskj.com/WeiXinResident/webservice/jkpg").methodNameAddress("Public_UpdateMyBlood").SoapObject(pram, "str").excute(this); } @OnClick({R.id.comit, R.id.title, R.id.phone}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.copy: // content.setText(moudleAdv.getText().toString()); // content.requestFocus(); break; case R.id.comit: reqComit(); break; case R.id.title: dismiss(); break; case R.id.phone: try { Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + "18765878758")); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } context.startActivity(intent); } catch (Exception e) { } break; } } @Override public void dismiss() { super.dismiss(); addressMap.onDestroy(); } }
3.调用方式:
PushCuffDialog dialog = new PushCuffDialog(context, sh); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)); } else { dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)); } dialog.show(); 本例子调用对象适合在service的和BradCastReiver 中直接调用调用前先判断权限否则崩溃系统