1.ÕâÊÇÒ»¶Î¿ìÊÖµÄÒâͼÌøתÀý×Ó
importClass(Packages.android.content.Intent);
importClass(Packages.android.net.Uri);
importClass(Packages.android.content.ActivityNotFoundException);
importClass(Packages.android.widget.Toast);
importClass(Packages.android.os.Handler);
importClass(Packages.android.os.Looper);
var userId = "2037335125";
var url = "kwai://profile/" + userId;
try {
var intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (e) {
if (e.toString().indexOf("android.content.ActivityNotFoundException") !== -1) {
showToast("δÕÒµ½¿ìÊÖÓ¦Óã¬ÇëÈ·±£ÒÑ°²×°¡£");
var webUrl = "https://www.kwai.com/profile/" + userId;
var webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUrl));
webIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(webIntent);
} else {
showToast("·¢Éú´íÎó: " + e.message);
}
}
function showToast(message) {
new Handler(Looper.getMainLooper()).post(function() {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
});
}
Õâ¶Î´úÂëÊÇÓÃÓÚÔÚ°²×¿É豸ÉÏͨ¹ýJava Script³¢ÊÔ´ò¿ª¿ìÊÖÓ¦ÓÃÖеÄij¸öÓû§¸öÈËÒ³Ãæ¡£Èç¹ûÉ豸ÉÏûÓа²×°¿ìÊÖÓ¦Óã¬Ôò»áÏÔʾһ¸ö¶ÌʱµÄToastÌáʾÐÅÏ¢£¬¸æÖªÓû§Î´ÕÒµ½¿ìÊÖÓ¦Ó㬲¢³¢ÊÔͨ¹ýä¯ÀÀÆ÷´ò¿ª¿ìÊÖµÄÍøÒ³°æÓû§¸öÈËÒ³Ãæ¡£ ´úÂëµÄÖ÷Òª²½ÖèÈçÏ£º - µ¼Èë±ØÒªµÄ°²×¿Àà¡£
- ÉèÖÃÒª·ÃÎʵĿìÊÖÓû§ID£¬²¢¹¹½¨¿ìÊÖÓ¦ÓÃÄÚÁ´½ÓURL¡£
- ´´½¨Ò»¸öIntent¶ÔÏó£¬ÉèÖÃΪ²é¿´¶¯×÷£¬²¢Ö¸¶¨URIΪ¿ìÊÖÓû§Ò³ÃæµÄURL¡£
- ÉèÖÃIntent±ê־ΪFLAG_ACTIVITY_NEW_TASK£¬ÒÔÈ·±£¼´Ê¹´Óºǫ́·þÎñÆô¶¯£¬Ò²ÄÜ´´½¨ÐµÄÈÎÎñÕ»¡£
- ʹÓÃcontext.startActivity()·½·¨³¢ÊÔÆô¶¯Intent¡£
- Èç¹ûÓöµ½android.content.ActivityNotFoundExceptionÒì³££¬±íʾÉ豸ÉÏûÓа²×°¿ìÊÖÓ¦Ó㬴Ëʱ»áÏÔʾһ¸öToastÌáʾÐÅÏ¢£¬²¢¹¹½¨¿ìÊÖÍøÒ³°æÓû§¸öÈËÒ³ÃæµÄURL£¬Ôٴδ´½¨Ò»¸öIntent¶ÔÏó³¢ÊÔ´ò¿ªÍøÒ³¡£
- Èç¹ûÓöµ½ÆäËûÒì³££¬»áÏÔʾ°üº¬´íÎóÐÅÏ¢µÄToastÌáʾ¡£
- ×Ô¶¨ÒåshowToast·½·¨£¬ÀûÓÃHandlerºÍLooperÈ·±£ToastÔÚÖ÷Ïß³ÌÖÐÏÔʾ£¬ÒòΪ°²×¿UI²Ù×÷±ØÐëÔÚÖ÷Ïß³ÌÖнøÐС£
2.ÕâÊǶ¶ÒôµÄÒâͼÌøתÀý×Ó¡£
importClass(Packages.android.content.Intent);
importClass(Packages.android.net.Uri);
importClass(Packages.android.content.ActivityNotFoundException);
importClass(Packages.android.widget.Toast);
importClass(Packages.android.os.Handler);
importClass(Packages.android.os.Looper);
var userId = "xyz123";
var url = "snssdk1128://aweme/detail/" + userId;
try {
var intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (e) {
if (e.toString().indexOf("android.content.ActivityNotFoundException") !== -1) {
showToast("δÕÒµ½¶¶ÒôÓ¦Óã¬ÇëÈ·±£ÒÑ°²×°¡£");
var webUrl = "https://www.douyin.com/user/" + userId;
var webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUrl));
webIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(webIntent);
} else {
showToast("·¢Éú´íÎó: " + e.message);
}
}
function showToast(message) {
new Handler(Looper.getMainLooper()).post(function() {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
});
}
´úÂë½á¹¹Ó빦Äܵ¼Èë±ØÒªµÄ°²×¿Àࣺ Intent £ºÓÃÓÚÆô¶¯»î¶¯¡£Uri £º´¦ÀíURI£¨Í³Ò»×ÊÔ´±êʶ·û£©¡£ActivityNotFoundException £ºÓÃÓÚ²¶»ñ»î¶¯Î´ÕÒµ½µÄÒì³£¡£Toast £ºÓÃÓÚÔÚÆÁÄ»ÉÏÏÔʾ¶ÌÔݵÄÏûÏ¢¡£Handler ºÍLooper £ºÓÃÓÚ´¦ÀíUIÏß³ÌÖеÄÈÎÎñ¡£
ÉèÖÃÓû§Ò³ÃæµÄURL£º userId Ö¸¶¨ÐèÒª·ÃÎʵĶ¶ÒôÓû§ID¡£url ¹¹½¨Òª·ÃÎʵÄURI£¬¸ñʽΪsnssdk1128://aweme/detail/{userId} ¡£
³¢ÊÔʹÓÃIntentÆô¶¯¶¶ÒôÓ¦Ó㺠- ´´½¨Ò»¸ö
Intent ¶ÔÏó£¬ÓÃÓÚÆô¶¯ACTION_VIEW ²Ù×÷£¬Í¬Ê±½âÎöΪ֮ǰ¹¹½¨µÄURL¡£ - ÉèÖñê־Ϊ
FLAG_ACTIVITY_NEW_TASK £¬È·±£»î¶¯ÔÚÐÂÈÎÎñÖÐÆô¶¯¡£ - µ÷ÓÃ
context.startActivity(intent) ³¢ÊÔÆô¶¯¶¶ÒôµÄ»î¶¯¡£
Òì³£´¦Àí£º - ²¶»ñ
ActivityNotFoundException £¬ÓÃÓÚ¼ì²âÉ豸ÉÏÊÇ·ñ°²×°Á˶¶ÒôÓ¦Óᣠ- Èç¹ûδ°²×°£¬ÏÔʾһ¸öToastÌáʾÓû§°²×°Ó¦Ó㬲¢Ìṩһ¸öÍøÒ³°æµÄÁ´½Ó×÷ΪÌæ´ú¡£Èç¹ûÍøÒ³Á´½ÓҲδÄÜ´¦Àí£¬½«Ê¹ÓÃ
showToast ·½·¨ÏÔʾ´íÎóÐÅÏ¢¡£
|