Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 實現桌面浮動小球與開機自啟動

看著手機上的 360 浮動小球,不評價其功能與實用性,至少在 UI 設計與交互上是不小的創新。

如圖片左上角所示,球中還會顯示手機當前運行狀況,向下拉動還會有彈射來達到加速、清理等目的。

那好,先來實現一個類似的小球(僅限於形狀,功能你懂的)。查閱資料後發現,整個界面除小球外,其他部分都做透明處理。

1、由於用到了 SYSTEM_ALERT_WINDOW,需在 AndroidManifest.xml 中添加權限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

要想在應用中被啟動為 Activity 或 Service,需要注册,這裡是 Service:

<service android:name=".Service1" />

2、界面透明化處理,整體代碼如下:

package com.XXX.autostart;

import android.view.View;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class Service1 extends Service
{
    LinearLayout mFloatLayout;
    WindowManager.LayoutParams wmParams;
    WindowManager mWindowManager;
    ImageButton mFloatView;

    @Override
    public void onCreate()
    {
        super.onCreate();
        createFloatView();
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    private void createFloatView()
    {
        wmParams = new WindowManager.LayoutParams();
        mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);
        wmParams.type = LayoutParams.TYPE_PHONE;
        wmParams.format = PixelFormat.RGBA_8888;
        wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        LayoutInflater inflater = LayoutInflater.from(getApplication());
        mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);
        mWindowManager.addView(mFloatLayout, wmParams);
        mFloatView = (ImageButton)mFloatLayout.findViewById(R.id.float_id);

        mFloatView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth() / 2;
                wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 25;
                mWindowManager.updateViewLayout(mFloatLayout, wmParams);
                return false;
            }
        });

        mFloatView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mFloatView.setVisibility(View.INVISIBLE);
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        mFloatView.setVisibility(View.VISIBLE);
                    }
                }, 3000);
            }
        });
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if(mFloatLayout != null)
        {
            mWindowManager.removeView(mFloatLayout);
        }
    }
}

這裡用 ImageButton 來實現小球,關鍵在於其顯示的圖片是圓形。因此,想讓浮動窗口呈現其他形狀,只需制作相應圖片賦給組件即可。功能上,這裡只實現了在屏幕上隨意拖動、單擊消失三秒後重現。

3、浮動小球有了,怎麼讓它啟動呢?注意上面 Service1 繼承的是 Service。在 MainActivity.java 中讓其顯現很簡單:

finish();
Intent intent = new Intent(getApplicationContext(), Service1.class);
startService(intent);

這裡 MainActivity 不需做任何處理,新建工程時默認即可。finish(); 可加可不加,加上後程序一運行就只剩小球,原界面消失。效果圖如下,圖形找的是紅色小火焰。

4、到此浮動小球就實現了,那怎麼讓它開機自啟動呢?其實很簡單,用到了 BroadcastReceiver。先添加權限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

另外,要在開機等時刻捕獲系統廣播,還需添加:

<receiver android:name=".BootBroadcastReceiver">
        <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
</receiver>

實現代碼如下:

package com.XXX.autostart;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootBroadcastReceiver extends BroadcastReceiver {

    static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(ACTION)) {
            Intent intent1 = new Intent(context, Service1.class);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(intent1);
        }
    }
}

把手機 ReBoot,可以發現紅色小球會自行啟動,只不過速度比 360 安全衛士慢一些。

更多Android相關信息見Android 專題頁面 https://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved