應用場景:
在一些場景中需要系統啟動以后就讓應用程序運行起來,如閘機打卡系統,智能點餐系統,廣告機系統等等。這個需求叫做自啟動。
除全國產版本核心板以外,RK3568核心板還有工業級,商業級,連接器和郵票孔多種版本。全國產版本核心板與其他版本核心板完全兼容。可以支持國產和非國產核心板的無縫切換,助力產品升級和國產化項目落地。
RK3568 Android11實現APP自啟動技術分享
平臺:迅為iTOP-3568開發板
■第一步:添加自啟服務
修改RK3568 Android11源碼frameworks\base\core\java\com\android\internal\app\ResolverActivity.java文件,添加如下藍色字體的代碼。
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
接下來將需要自啟動的APP放到device/rockchip/rk356x/rk3568_r/preinstall_del/目錄下,如果沒有preinstall_del目錄的話需要手動新建一個preinstall_del目錄即可,然后添加以下文件Android.mk,xxx.apk(APP),preinstall.mk這三個文件。
Android.mk文件內容如下所示:
include $(call all-subdir-makefiles)
preinstall.mk文件內容:
PRODUCT_PACKAGES += xxx
xxx為APP名字,例如demo.apk。這份目錄不僅是APK的預裝指定位置,當您希望新增APK進行預裝時,可將該APK文件復制至“preinstall_del/”目錄下,并隨后將APK的名稱增補至“preinstall.mk”配置文件中,以完成預安裝的準備工作。這樣的操作確保了APK能夠按照預期被系統識別并預裝至目標設備中。
■第三步:添加APP包名
修改RK3568Android11源碼文件device/rockchip/rk356x/rk3568_r/rk3568_r.mk,例如包名為:com.example.demo,在rk3568_r.mk中下面藍色字體代碼。
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源碼,然后燒寫Android鏡像進行測試。
E
-
開發板
+關注
關注
25文章
5032瀏覽量
97371 -
RK3568
+關注
關注
4文章
514瀏覽量
5034 -
迅為電子
+關注
關注
0文章
31瀏覽量
26
發布評論請先 登錄
相關推薦
評論