Android 實(shí)現(xiàn)自動(dòng)啟動(dòng)無(wú)界面,點(diǎn)擊圖標(biāo)有界面思路

項(xiàng)目需求:
點(diǎn)擊圖標(biāo)有界面,自動(dòng)運(yùn)行或者開(kāi)發(fā)點(diǎn)擊運(yùn)行項(xiàng)目無(wú)界面

這個(gè)需求困擾了我很久,2天多吧 后來(lái)?yè)Q了一個(gè)思路就實(shí)現(xiàn)了

效果圖:


2222222.gif

就是弄個(gè)全局變量,在就是開(kāi)啟一個(gè)服務(wù)里去賦值就行...
我之前想的是怎么獲取APP圖標(biāo),在去監(jiān)聽(tīng)這個(gè)圖標(biāo)的事件,在給全局變量賦值,之后再依據(jù)全局變量去判斷。

image.png

代碼如下

package com.test.testdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (!Config.runFlag){
            Log.i("TAG", "自動(dòng)或者點(diǎn)擊運(yùn)行無(wú)界面---- ");
            Intent intent = new Intent(this, TestService.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startService(intent);
            finish();
        }
    }
}


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點(diǎn)擊圖標(biāo)啟動(dòng)你才會(huì)看見(jiàn)我"
        android:textSize="32sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>



package com.test.testdemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

/**
 * @auther eerdunsang
 * @date 2021/3/8
 * @time 14:20.
 */
public class TestService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Config.runFlag = true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Config.runFlag = false;
    }
}


package com.test.testdemo;

/**
 * @auther eerdunsang
 * @date 2021/3/8
 * @time 14:21.
 */
public class Config {
    public static boolean runFlag = false;
}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.testdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service android:name=".TestService"/>
    </application>

</manifest>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容