應(yīng)用場(chǎng)景:
在一些場(chǎng)景中需要系統(tǒng)啟動(dòng)以后就讓?xiě)?yīng)用程序運(yùn)行起來(lái),如閘機(jī)打卡系統(tǒng),智能點(diǎn)餐系統(tǒng),廣告機(jī)系統(tǒng)等等。這個(gè)需求叫做自啟動(dòng)。
除全國(guó)產(chǎn)版本核心板以外,RK3568核心板還有工業(yè)級(jí),商業(yè)級(jí),連接器和郵票孔多種版本。全國(guó)產(chǎn)版本核心板與其他版本核心板完全兼容??梢灾С謬?guó)產(chǎn)和非國(guó)產(chǎn)核心板的無(wú)縫切換,助力產(chǎn)品升級(jí)和國(guó)產(chǎn)化項(xiàng)目落地。
RK3568 Android11實(shí)現(xiàn)APP自啟動(dòng)技術(shù)分享
平臺(tái):迅為iTOP-3568開(kāi)發(fā)板
■第一步:添加自啟服務(wù)
修改RK3568 Android11源碼frameworks\base\core\java\com\android\internal\app\ResolverActivity.java文件,添加如下藍(lán)色字體的代碼。
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import android.os.SystemProperties;
import android.app.role.RoleManager;
/**
* This activity is displayed when the system attempts to start an Intent for
@@ -341,6 +345,18 @@ public class ResolverActivity extends Activity implements
protected void onCreate(Bundle savedInstanceState, Intent intent,
CharSequence title, int defaultTitleRes, Intent[] initialIntents,
List rList, boolean supportsAlwaysUseOption) {
String LauncherPkg = SystemProperties.get("persist.sys.def_launherpag");
if(LauncherPkg!=null/* && !isDefaultLauncher(this,LauncherPkg)*/){
Log.v("defaultLauncher","-----found default launcher pkg:"+LauncherPkg);
setDefaltLauncher(this,LauncherPkg);
startDefaultLauncher(this,LauncherPkg);
finish();
return;
}
setTheme(appliedThemeResId());
super.onCreate(savedInstanceState);
@@ -433,6 +449,47 @@ public class ResolverActivity extends Activity implements
intent.getAction() + ":" + intent.getType() + ":"
+ (categories != null ? Arrays.toString(categories.toArray()) : ""));
}
public void setDefaltLauncher(Context context, String packageName) {
String roleName = "android.app.role.HOME";
boolean add = true;
int flags = 0;
UserHandle user = android.os.Process.myUserHandle();
RoleManager roleManager = context.getSystemService(RoleManager.class);
Executor executor = context.getMainExecutor();
Consumer< Boolean > callback = successful -> {
if (successful) {
Log.v("defaultLauncher","set Defalt Launcher successful" );
} else {
Log.v("defaultLauncher","set Defalt Launcher failed ");
}
};
roleManager.addRoleHolderAsUser(roleName, packageName, flags, user, executor, callback);
}
public void startDefaultLauncher(Context context, String packageName) {
Intent intent = new Intent();
PackageManager pm = context.getPackageManager();
intent = pm.getLaunchIntentForPackage(packageName);
if (intent != null)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch (Exception e) {
Log.v("defaultLauncher","start Default Launcher failed,not find package:"+packageName );
}
}
public boolean isDefaultLauncher(Context context,String launcherPkg){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
String currentHomePackage = resolveInfo.activityInfo.packageName;
Log.v("defaultLauncher","-currentHomePackage:"+currentHomePackage);
return launcherPkg.equals(currentHomePackage);
}
private boolean isIntentPicker() {
return getClass().equals(ResolverActivity.class);
■第二步:添加APP
接下來(lái)將需要自啟動(dòng)的APP放到device/rockchip/rk356x/rk3568_r/preinstall_del/目錄下,如果沒(méi)有preinstall_del目錄的話需要手動(dòng)新建一個(gè)preinstall_del目錄即可,然后添加以下文件Android.mk,xxx.apk(APP),preinstall.mk這三個(gè)文件。
Android.mk文件內(nèi)容如下所示:
include $(call all-subdir-makefiles)
preinstall.mk文件內(nèi)容:
PRODUCT_PACKAGES += xxx
xxx為APP名字,例如demo.apk。這份目錄不僅是APK的預(yù)裝指定位置,當(dāng)您希望新增APK進(jìn)行預(yù)裝時(shí),可將該APK文件復(fù)制至“preinstall_del/”目錄下,并隨后將APK的名稱增補(bǔ)至“preinstall.mk”配置文件中,以完成預(yù)安裝的準(zhǔn)備工作。這樣的操作確保了APK能夠按照預(yù)期被系統(tǒng)識(shí)別并預(yù)裝至目標(biāo)設(shè)備中。
■第三步:添加APP包名
修改RK3568Android11源碼文件device/rockchip/rk356x/rk3568_r/rk3568_r.mk,例如包名為:com.example.demo,在rk3568_r.mk中下面藍(lán)色字體代碼。
PRODUCT_PROPERTY_OVERRIDES += persist.sys.timezone=Asia/Shanghai
PRODUCT_PROPERTY_OVERRIDES += ro.vendor.hdmirotationlock=true
#PRODUCT_PROPERTY_OVERRIDES += vendor.hwc.device.primary=HDMI-A-1
PRODUCT_PROPERTY_OVERRIDES += vendor.hwc.device.primary=DSI
PRODUCT_PROPERTY_OVERRIDES += persist.sys.def_launherpag=com.example.demo
修改完畢之后,保存修改的文件,重新編譯Android源碼,然后燒寫(xiě)Android鏡像進(jìn)行測(cè)試。
E
-
開(kāi)發(fā)板
+關(guān)注
關(guān)注
25文章
5032瀏覽量
97371 -
RK3568
+關(guān)注
關(guān)注
4文章
514瀏覽量
5031 -
迅為電子
+關(guān)注
關(guān)注
0文章
31瀏覽量
26
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論