Android 应用开机自启和无需权限开启悬浮框

开机自启主要自定义广播接收类,且需要在清单文件中注册,不要在代码中动态注册。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
<receiver android:name=".***">
     <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED"/>
          <category android:name="android.intent.category.LAUNCHER"/>
     </intent-filter>
</receiver>
<service android:name=".***"/>

开始编写广播接收器:

public class MyBroadcastRecevice extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
            // TODO: 2016/9/18
            context.startService(new Intent(context,***));
        }
    }
}

再看来看看在服务中启动悬浮框的代码:

/**
     * 创建悬浮框
     */
    public void createFloatingBoxView() {
        wmParams = ((AppContext) getApplication()).getMywmParams();
        mWindowManager = (WindowManager) getApplication().getSystemService(
                Application.WINDOW_SERVICE);
        wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;
        wmParams.format = PixelFormat.RGBA_8888;
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        wmParams.gravity = Gravity.TOP | Gravity.LEFT;
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        floatingBoxView = new FloatingBoxView(getApplicationContext(),this);
        mWindowManager.addView(floatingBoxView, wmParams);
        //这两行负责监听的,别理他
        floatingBoxView.setMoveListener(this);
        floatingBoxView.setAllClickListener(this);
    }

 

posted @ 2018-06-27 12:53  星辰之力  阅读(569)  评论(0编辑  收藏  举报