美文网首页面试
我要做 Android 第四弹 ps(Q:如何启动其他应用的Ac

我要做 Android 第四弹 ps(Q:如何启动其他应用的Ac

作者: Jiwenjie | 来源:发表于2018-09-07 11:44 被阅读16次

    一、打开第三方应用
    (1)

    Intent intent=new Intent();  
        //包名 包名+类名(全路径)  
        intent.setClassName("com.jack", "com.jack.PlaneActivity");  
        startActivity(intent);  
    

    (2)

        Intent intent = new Intent();  
        ComponentName comp = new                         ComponentName("com.jack","com.jack.PlaneActivity");         
        intent.setComponent(comp);  
        intent.setAction("android.intent.action.MAIN");  
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
        startActivity(intent);
    

    (3)转载自https://blog.csdn.net/aaa111/article/details/41833189

    public static final String APP_PACKAGE_NAME = "com.*.*";//包名
    
        /**
         * 启动薄荷App
         * @param context
         */
        public static void launchapp(Context context) {
            // 判断是否安装过App,否则去市场下载
            if (isAppInstalled(context, APP_PACKAGE_NAME)) {
                context.startActivity(context.getPackageManager().getLaunchIntentForPackage(APP_PACKAGE_NAME));
            } else {
                goToMarket(context, APP_PACKAGE_NAME);
            }
        }
    
        /**
         * 检测某个应用是否安装
         * 
         * @param context
         * @param packageName
         * @return
         */
        public static boolean isAppInstalled(Context context, String packageName) {
            try {
                context.getPackageManager().getPackageInfo(packageName, 0);
                return true;
            } catch (NameNotFoundException e) {
                return false;
            }
        }
    
        /**
         * 去市场下载页面
         */
        public static void goToMarket(Context context, String packageName) {
            Uri uri = Uri.parse("market://details?id=" + packageName);
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
            try {
                context.startActivity(goToMarket);
            } catch (ActivityNotFoundException e) {
            }
        }
    

    调用系统应用

    (1)从 google 搜索内容

    Intent intent = new Intent();  
        intent.setAction(Intent.ACTION_WEB_SEARCH);  
        intent.putExtra(SearchManager.QUERY, "搜索内容")  
        startActivity(intent);  
    

    (2)浏览网页

    Uri uri = Uri.parse("http://www.google.com");  
        Intent it = new Intent(Intent.ACTION_VIEW, uri);  
        startActivity(it);  
    
    

    (3)显示地图

    Uri uri = Uri.parse("geo:38.899533,-77.036476");  
        Intent it = newIntent(Intent.Action_VIEW,uri);  
        startActivity(it);  
    

    (4)拨打电话

    Uri uri =Uri.parse("tel:xxxxxx");  
        Intent it = new Intent(Intent.ACTION_DIAL,uri);    
        startActivity(it);  
    

    (5)发短信

    //方法1:  
        Intent it = newIntent(Intent.ACTION_VIEW);     
        it.putExtra("sms_body", "TheSMS text");     
        it.setType("vnd.android-dir/mms-sms");     
        startActivity(it);  
    
        //方法2:  
        Uri uri =Uri.parse("smsto:0800000123");     
        Intent it = newIntent(Intent.ACTION_SENDTO, uri);     
        it.putExtra("sms_body", "TheSMS text");     
        startActivity(it);  
    
        //方法三:  
        String body="this is sms demo";  
        Intent mmsintent = newIntent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null));  
        mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY,body);  
        mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE,true);  
        mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT,true);  
        startActivity(mmsintent);  
    

    (6)发 Email

        Uri uri = Uri.parse("mailto:xxx@abc.com");  
        Intent it = newIntent(Intent.ACTION_SENDTO, uri);  
        startActivity(it);  
    
        Intent it = new Intent(Intent.ACTION_SEND);     
        it.putExtra(Intent.EXTRA_EMAIL,"me@abc.com");     
        it.putExtra(Intent.EXTRA_TEXT, "Theemail body text");     
        it.setType("text/plain");     
        startActivity(Intent.createChooser(it,"Choose Email Client"));  
    
        Intent it=new Intent(Intent.ACTION_SEND);       
        String[] tos={"me@abc.com"};       
        String[]ccs={"you@abc.com"};       
        it.putExtra(Intent.EXTRA_EMAIL, tos);       
        it.putExtra(Intent.EXTRA_CC, ccs);       
        it.putExtra(Intent.EXTRA_TEXT, "Theemail body text");       
        it.putExtra(Intent.EXTRA_SUBJECT, "Theemail subject text");       
        it.setType("message/rfc822");       
        startActivity(Intent.createChooser(it,"Choose Email Client"));     
    
        Intent it = newIntent(Intent.ACTION_SEND);     
        it.putExtra(Intent.EXTRA_SUBJECT, "Theemail subject text");      
        it.putExtra(Intent.EXTRA_STREAM,"file:///sdcard/mysong.mp3");     
        sendIntent.setType("audio/mp3");     
        startActivity(Intent.createChooser(it,"Choose Email Client"));
    

    (7)播放多媒体

    Intent it = new Intent(Intent.ACTION_VIEW);  
        Uri uri =Uri.parse("file:///sdcard/song.mp3");  
        it.setDataAndType(uri,"audio/mp3");  
        startActivity(it);  
        Uri uri =Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"1");     
        Intent it = new Intent(Intent.ACTION_VIEW,uri);     
        startActivity(it);  
    

    (8)卸载 apk

     Uri uri =Uri.fromParts("package", strPackageName, null);     
        Intent it = newIntent(Intent.ACTION_DELETE, uri);     
        startActivity(it);  
    

    (9)安装 apk

    Uri installUri = Uri.fromParts("package","xxx", null);  
        returnIt = newIntent(Intent.ACTION_PACKAGE_ADDED, installUri);  
    
        Intent intent = new Intent(Intent.ACTION_VIEW);  
        intent.setDataAndType(Uri.parse("file://" + filepath),"application/vnd.android.package-archive");  
        startActivity(intent);// 安装 
    
    

    (10)显示应用详细列表

    Uri uri =Uri.parse("market://details?id=app_id");          
        Intent it = new Intent(Intent.ACTION_VIEW,uri);          
        startActivity(it);          
        //where app_id is the application ID, findthe ID           
        //by clicking on your application on Markethome           
        //page, and notice the ID from the addressbar  
        //发现用package name也可以  
        //Uri uri =Uri.parse("market://details?id=<packagename>");  
    

    (11)寻找应用

    Uri uri =Uri.parse("market://search?q=pname:pkg_name");          
        Intent it = new Intent(Intent.ACTION_VIEW,uri);          
        startActivity(it);  
        //where pkg_name is the full package pathfor an application  
    

    (12)打开联系人列表

     //1             
        Intent i = new Intent();  
        i.setAction(Intent.ACTION_GET_CONTENT);  
        i.setType("vnd.android.cursor.item/phone");  
        startActivityForResult(i, REQUEST_TEXT);  
        //2  
        Uri uri = Uri.parse("content://contacts/people");  
        Intent it = new Intent(Intent.ACTION_PICK, uri);  
        startActivityForResult(it, REQUEST_TEXT);  
    

    这篇文章是转载的,出处在这 https://blog.csdn.net/aaa111/article/details/41833189,随着 Android 版本的提高,权限也有很多的变化,所以可能会有一些方法不能使用,不过这里可以作为参考。也算是弥补了以下基本知识。再说了,记不得 Google 不就 OK 了是吧。

    愿我们成为真实的自己。

    相关文章

      网友评论

        本文标题:我要做 Android 第四弹 ps(Q:如何启动其他应用的Ac

        本文链接:https://www.haomeiwen.com/subject/rbtcgftx.html