开始将Chrome作为web应用在Android上启动 [英] Start Chrome as Web-App on Android start
问题描述
我有一个非常具体的问题。我已经意识到在Android平板电脑的web应用程序,这将在展览(Outform iDisplay)一起使用。出于这个原因,Web应用程序有直接开机后启动。后启动的事情是没有问题的(用android.permission.RECEIVE_BOOT_COMPLETED广播),但我要开始的是Chrome网络应用程序有问题。为获得意图,我读了发射收藏夹的图标,这个片段:
//奇巧,因此launcher3
URL =内容://com.android.launcher3.settings/favorites通知=真的吗? ContentResolver的解析器= getContentResolver();
光标光标= resolver.query(Uri.parse(URL),NULL,NULL,NULL,NULL); 如果(光标= NULL&放大器;!&安培; cursor.moveToFirst())
{
做
{
串ENT1 = cursor.getString(0);
串ENT2 = cursor.getString(1);
串ent3 = cursor.getString(2); //还有就是意图字符串
串ent4 = cursor.getString(3);
的System.out.println(测试);
串ent5 = cursor.getString(4);
串ent6 = cursor.getString(5);
串ent7 = cursor.getString(6);
串ent8 = cursor.getString(7);
串ent9 = cursor.getString(8);
串ent10 = cursor.getString(9);
串ent11 = cursor.getString(10);
串ent12 = cursor.getString(11);
串ent14 = cursor.getString(13);
串ent15 = cursor.getString(14);
串ent17 = cursor.getString(16);
串ent18 = cursor.getString(17);
字符串ent19 = cursor.getString(18);
串ent20 = cursor.getString(19);
如果(ent2.equals(历史书))//获取正确的意图
{
runAction = ent3;
}
的System.out.println(ent3);
}而(cursor.moveToNext());
}
意图字符串包含这样的:
<$p$p><$c$c>#Intent;action=com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP;package=com.android.chrome;S.org.chromium.chrome.browser.webapp_title=History%20Book;S.org.chromium.chrome.browser.webapp_id=86e362e4-a25d-4142-8a32-c02ffcb176a9;i.org.chromium.content_public.common.orientation=6;S.org.chromium.chrome.browser.webapp_icon=;S.org.chromium.chrome.browser.webapp_mac=3ZaXFbyWnJQaqFFOuUj3OssNz7DrBaaiWfzO2Dd7VIU%3D%0A;S.org.chromium.chrome.browser.webapp_url=http%3A%2F%2F192.168.5.148%2Fstyria%2Fhistorybook%2Findex.html;end这看起来相当不错,但我怎么能在一个小的应用程序,刚刚有一个目的启动这个意图开始像这样的意图是什么?
就在年底小记:我曾尝试收拾这个东西变成一个web视图,但web视图不断死了,因为一个错误的libc的,所以这对我来说没有选项
最后我得到这个东西的工作。我是在正确的道路,但有些Chrome.apk逆向工程帮助我的最后一英里。
我在的onCreate创建具有以下code虚活动:
搜索主屏幕上的权项,在我的案件的AOSP发射3:
//搜索历史书上的快捷方式的主屏幕
字符串URL =;
串runAction =; 最后弦乐AUTHORITY =com.android.launcher3.settings;
最后乌里CONTENT_URI = Uri.parse(内容://+
AUTHORITY +/我的最爱通知=真的吗?); 最后ContentResolver的CR = getContentResolver();
光标光标= cr.query(CONTENT_URI,NULL,NULL,NULL,NULL); cursor.moveToFirst();
做{
字符串ID = cursor.getString(cursor.getColumnIndex(_ ID));
字符串title = cursor.getString(cursor.getColumnIndex(标题));
字符串意图= cursor.getString(cursor.getColumnIndex(意图));
如果(title.equals(getResources()。的getString(R.string.homescreen_link)))
{
runAction =意图;
} }而(cursor.moveToNext());
在这一点上,我希望有意向的字符串。因此,解析字符串,并创建一个新的意图:
意向意图=新的Intent();
intent.setAction(com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP);
intent.setPackage(com.android.chrome);
intent.setClassName(com.android.chrome,com.google.android.apps.chrome.webapps.WebappManager); HashMap的&LT;字符串,字符串&GT; intentVals = getIntentParams(runAction); intent.putExtra(\"org.chromium.chrome.browser.webapp_title\",intentVals.get(\"S.org.chromium.chrome.browser.webapp_title\"));
intent.putExtra(\"org.chromium.chrome.browser.webapp_icon\",intentVals.get(\"S.org.chromium.chrome.browser.webapp_icon\"));
intent.putExtra(\"org.chromium.chrome.browser.webapp_id\",intentVals.get(\"S.org.chromium.chrome.browser.webapp_id\"));
intent.putExtra(\"org.chromium.chrome.browser.webapp_url\",intentVals.get(\"S.org.chromium.chrome.browser.webapp_url\"));
intent.putExtra(\"org.chromium.chrome.browser.webapp_mac\",intentVals.get(\"S.org.chromium.chrome.browser.webapp_mac\"));
INT方向= 6;
尝试
{
方向=的Integer.parseInt(intentVals.get(i.org.chromium.content_public.common.orientation));
}
赶上(NumberFormatException的_nex)
{
Log.e(TAG,误格式化,使用默认设置(6));
}
intent.putExtra(org.chromium.content_public.common.orientation,方向); 尝试
{
字节[] = abyte0 Base64.de code(
intentVals.get(S.org.chromium.chrome.browser.webapp_mac),
0);
的System.out.println(新的String(abyte0));
}
赶上(抛出:IllegalArgumentException _iae)
{
Log.e(TAG,
错webapp_mac:
+ intentVals
获得(S.org.chromium.chrome.browser.webapp_mac));
} startActivity(意向);
完();
和这个函数解析意图参数出来的意图字符串:
私人的HashMap&LT;字符串,字符串&GT; getIntentParams(字符串_runAction)
{
HashMap的&LT;字符串,字符串&GT; retMap =新的HashMap&LT;字符串,字符串&GT;(); 的String [] =对_runAction.split();
的for(int i = 0; I&LT; pairs.length;我++)
{
串[] KEYVAL =对[I] .split(=);
如果(keyval.length == 2)
{
字符串键= KEYVAL [0];
字符串值=;
尝试
{
值= java.net.URLDe coder.de code(KEYVAL [1],UTF-8);
}
赶上(UnsupportedEncodingException _uee)
{
Log.e(TAG,不支持的编码:+ _uee.getMessage());
}
retMap.put(键,值);
}
}
返回retMap;
}
和在res /值的strings.xml:
&LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;资源&GT;
&LT;字符串名称=APP_NAME&GT; WebAppStarter&LT; /串&GT;
&LT;字符串名称=homescreen_link&GT;历史书&LT; /串&GT;
&LT; /资源&GT;
就是这样。您可以配置主屏幕的链接名称的strings.xml搜索。当应用程序找到字符串,它解析字符串的意图,并创建一个新的意图Chrome启动以全屏显示活动Web应用程序。
i have a quite specific problem. I have realized a Web App on an Android tablet, which will be used on an exhibition (Outform iDisplay). For this reason, the Web App has to start directly after boot. The after-boot thing is no problem (Broadcast with "android.permission.RECEIVE_BOOT_COMPLETED"), but i have a problem to start Chrome as Web-App. For getting the Intent, i have read the Icons in the launcher favorites with this snippet:
//Kitkat, therefore launcher3
url = "content://com.android.launcher3.settings/favorites?Notify=true";
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), null, null, null, null);
if (cursor != null && cursor.moveToFirst())
{
do
{
String ent1 = cursor.getString(0);
String ent2 = cursor.getString(1);
String ent3 = cursor.getString(2); //there is the Intent string
String ent4 = cursor.getString(3);
System.out.println("Test");
String ent5 = cursor.getString(4);
String ent6 = cursor.getString(5);
String ent7 = cursor.getString(6);
String ent8 = cursor.getString(7);
String ent9 = cursor.getString(8);
String ent10 = cursor.getString(9);
String ent11 = cursor.getString(10);
String ent12 = cursor.getString(11);
String ent14 = cursor.getString(13);
String ent15 = cursor.getString(14);
String ent17 = cursor.getString(16);
String ent18 = cursor.getString(17);
String ent19 = cursor.getString(18);
String ent20 = cursor.getString(19);
if(ent2.equals("History Book")) //Get the right intent
{
runAction = ent3;
}
System.out.println(ent3);
} while (cursor.moveToNext());
}
The Intent string contains something like this:
#Intent;action=com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP;package=com.android.chrome;S.org.chromium.chrome.browser.webapp_title=History%20Book;S.org.chromium.chrome.browser.webapp_id=86e362e4-a25d-4142-8a32-c02ffcb176a9;i.org.chromium.content_public.common.orientation=6;S.org.chromium.chrome.browser.webapp_icon=;S.org.chromium.chrome.browser.webapp_mac=3ZaXFbyWnJQaqFFOuUj3OssNz7DrBaaiWfzO2Dd7VIU%3D%0A;S.org.chromium.chrome.browser.webapp_url=http%3A%2F%2F192.168.5.148%2Fstyria%2Fhistorybook%2Findex.html;end
This looks quite good, but how can i start an Intent like this in a small app, which just has the single purpose to start this intent?
Just a small note at the end: I have tried to pack this thing into a webview, but the webview died constantly because of an libc error, so this is no option for me.
Finally i got this thing working. I was on the right way, but some Chrome.apk reverse engineering helped me for the last mile. I have created a dummy activity with the following code in onCreate:
Search for the right entry on the homescreen, in my case for the AOSP launcher 3:
//Search for the History Book Shortcut on the Homescreen
String url = "";
String runAction="";
final String AUTHORITY = "com.android.launcher3.settings";
final Uri CONTENT_URI = Uri.parse("content://" +
AUTHORITY + "/favorites?notify=true");
final ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(CONTENT_URI,null,null,null,null);
cursor.moveToFirst();
do {
String id = cursor.getString(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String intent = cursor.getString(cursor.getColumnIndex("intent"));
if(title.equals(getResources().getString(R.string.homescreen_link)))
{
runAction = intent;
}
} while (cursor.moveToNext());
At this point, i have hopefully the intent as string. So, parse the string and create a new intent:
Intent intent = new Intent();
intent.setAction("com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP");
intent.setPackage("com.android.chrome");
intent.setClassName("com.android.chrome", "com.google.android.apps.chrome.webapps.WebappManager");
HashMap<String, String> intentVals = getIntentParams(runAction);
intent.putExtra("org.chromium.chrome.browser.webapp_title",intentVals.get("S.org.chromium.chrome.browser.webapp_title"));
intent.putExtra("org.chromium.chrome.browser.webapp_icon",intentVals.get("S.org.chromium.chrome.browser.webapp_icon"));
intent.putExtra("org.chromium.chrome.browser.webapp_id",intentVals.get("S.org.chromium.chrome.browser.webapp_id"));
intent.putExtra("org.chromium.chrome.browser.webapp_url",intentVals.get("S.org.chromium.chrome.browser.webapp_url"));
intent.putExtra("org.chromium.chrome.browser.webapp_mac",intentVals.get("S.org.chromium.chrome.browser.webapp_mac"));
int orientation = 6;
try
{
orientation = Integer.parseInt(intentVals.get("i.org.chromium.content_public.common.orientation"));
}
catch(NumberFormatException _nex)
{
Log.e(TAG, "Wrong format, using default (6)");
}
intent.putExtra("org.chromium.content_public.common.orientation", orientation);
try
{
byte[] abyte0 = Base64.decode(
intentVals.get("S.org.chromium.chrome.browser.webapp_mac"),
0);
System.out.println(new String(abyte0));
}
catch (IllegalArgumentException _iae)
{
Log.e(TAG,
"Wrong webapp_mac: "
+ intentVals
.get("S.org.chromium.chrome.browser.webapp_mac"));
}
startActivity(intent);
finish();
And this function parses the intent parameters out of the intent string:
private HashMap<String, String> getIntentParams(String _runAction)
{
HashMap<String, String> retMap = new HashMap<String, String>();
String[] pairs = _runAction.split(";");
for (int i = 0; i < pairs.length; i++)
{
String[] keyval = pairs[i].split("=");
if(keyval.length==2)
{
String key = keyval[0];
String value = "";
try
{
value = java.net.URLDecoder.decode(keyval[1], "UTF-8");
}
catch (UnsupportedEncodingException _uee)
{
Log.e(TAG, "Unsupported Encoding: " + _uee.getMessage());
}
retMap.put(key, value);
}
}
return retMap;
}
And the strings.xml in res/values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WebAppStarter</string>
<string name="homescreen_link">History Book</string>
</resources>
That's it. You can configure the Homescreen link name to search for in strings.xml. When the app finds the string, it parses the intent string and creates a new intent to start Chrome as a Full Screen Activity Web App.
这篇关于开始将Chrome作为web应用在Android上启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!