Android的intent

本文详细介绍Android中Intent的使用方法,包括启动Activity、显示网页、地图、路径规划等常见操作,并提供多个实例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个 Intent对象 包含了一组信息:
1. Component name  指定我要启动哪一个组件
2. Action  (有一系列的大写的常量)
3. Data
(URI)
4. Category
5. Extras
6. Flags

来一个简单的例子:在一个主Activty里来呼叫另一个Activity
首先我们来熟悉Activity类的架构是(粗体是必须要有的)
public class Androidnew01Activity extends Activity {
    private static String TAG="Androidnew01Activity";
    private Button mybtn1 = null;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       
        mybtn1 = (Button)this.findViewById(R.id.btn1);
        mybtn1.setOnClickListener(new MyBtnListener());
    }
   
    class MyBtnListener implements Button.OnClickListener{

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Log.i(TAG, "in class: MyBtnListener...");
            Intent intent = new Intent(Androidnew01Activity.this,
                    LoginWindow.class);
            startActivity(intent);

        }
       
    }
 }

上面蓝色字体是启动一个新Activity的过程, 可见我们要启动的是LoginWindow这个Acitvity, 所以我们需要先:
1. 创建类 LoginWindow 继承自Activity
2.  定义一个layout的xml文件,并在类LoginWindow中以setContentView() 方式加入onCreate()中
2. 在manifest中定义LoginWindow这个 <activity>
3. 在类LoginWindow的适当时机(比如按钮或其它),通过创建和使用intent 来启动LoginWindows这个Activity

通过Intent启动一个Activity时,我们可以发现按返回键可以返回主Activty, 但是如果此时直接按Home键, 实际上并没有退出程序。
实际实验,调出LoginWindow后, 先按Home, 再从系统列表中启动这个程序,发现仍然在LoginWindow, 而不是重新从主Activty启动!!!说明刚才的Home键没有退出程序,只有一步一步通过返回键, 才能退出程序。
=================================================================
附:

Android Intent的几种用法全面总结

Intent应该算是Android中特有的东西。你可以在Intent中指定程序要执行的动作(比如:view,edit,dial),以及程序执行到该动作时所需要的资料。都指定好后,只要调用startActivity(),Android系统会自动寻找最符合你指定要求的应用程序,并执行该程序。

下面列出几种Intent的用法
显示网页:
  1. Uri uri = Uri.parse("http://www.google.com");
  2. Intent it  = new Intent(Intent.ACTION_VIEW,uri);
  3. startActivity(it);
复制代码
显示地图:
  1. Uri uri = Uri.parse("geo:38.899533,-77.036476");
  2. Intent it = new Intent(Intent.Action_VIEW,uri);
  3. startActivity(it);
复制代码
路径规划:
  1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
  2. Intent it = new Intent(Intent.ACTION_VIEW,URI);
  3. startActivity(it);
复制代码
拨打电话:
调用拨号程序
  1. Uri uri = Uri.parse("tel:xxxxxx");
  2. Intent it = new Intent(Intent.ACTION_DIAL, uri);  
  3. startActivity(it);  
复制代码
  1. Uri uri = Uri.parse("tel.xxxxxx");
  2. Intent it =new Intent(Intent.ACTION_CALL,uri);
  3. 要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />
复制代码
发送SMS/MMS
调用发送短信的程序
  1. Intent it = new Intent(Intent.ACTION_VIEW);   
  2. it.putExtra("sms_body", "The SMS text");   
  3. it.setType("vnd.android-dir/mms-sms");   
  4. startActivity(it);  
复制代码
发送短信
  1. Uri uri = Uri.parse("smsto:0800000123");   
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);   
  3. it.putExtra("sms_body", "The SMS text");   
  4. startActivity(it);  
复制代码
发送彩信
  1. Uri uri = Uri.parse("content://media/external/images/media/23");   
  2. Intent it = new Intent(Intent.ACTION_SEND);   
  3. it.putExtra("sms_body", "some text");   
  4. it.putExtra(Intent.EXTRA_STREAM, uri);   
  5. it.setType("image/png");   
  6. startActivity(it);
复制代码
发送Email

  1. Uri uri = Uri.parse("mailto:xxx@abc.com");
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  3. startActivity(it);
复制代码
  1. Intent it = new Intent(Intent.ACTION_SEND);   
  2. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");   
  3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");   
  4. it.setType("text/plain");   
  5. startActivity(Intent.createChooser(it, "Choose Email Client"));  
复制代码
  1. Intent it=new Intent(Intent.ACTION_SEND);     
  2. String[] tos={"me@abc.com"};     
  3. String[] ccs={"you@abc.com"};     
  4. it.putExtra(Intent.EXTRA_EMAIL, tos);     
  5. it.putExtra(Intent.EXTRA_CC, ccs);     
  6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");     
  7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     
  8. it.setType("message/rfc822");     
  9. startActivity(Intent.createChooser(it, "Choose Email Client"));   
复制代码
添加附件
  1. Intent it = new Intent(Intent.ACTION_SEND);   
  2. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
  3. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");   
  4. sendIntent.setType("audio/mp3");   
  5. startActivity(Intent.createChooser(it, "Choose Email Client"));
复制代码
播放多媒体
  1.   
  2. Intent it = new Intent(Intent.ACTION_VIEW);
  3. Uri uri = Uri.parse("file:///sdcard/song.mp3");
  4. it.setDataAndType(uri, "audio/mp3");
  5. startActivity(it);
复制代码
  1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);   
  3. startActivity(it);  
复制代码
Uninstall 程序
  1. Uri uri = Uri.fromParts("package", strPackageName, null);   
  2. Intent it = new Intent(Intent.ACTION_DELETE, uri);   
  3. startActivity(it);
复制代码
uninstall apk
  1. Uri uninstallUri = Uri.fromParts("package", "xxx", null);

  2. returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
复制代码

install apk
  1. Uri installUri = Uri.fromParts("package", "xxx", null);

  2. returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
复制代码


play audio
  1. Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");

  2. returnIt = new Intent(Intent.ACTION_VIEW, playUri);
复制代码
  1. //发送附件
  2. Intent it = new Intent(Intent.ACTION_SEND);  
  3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  
  4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3");  
  5. sendIntent.setType("audio/mp3");  
  6. startActivity(Intent.createChooser(it, "Choose Email Client"));
  1. //搜索应用
  2. Uri uri = Uri.parse("market://search?q=pname:pkg_name");  
  3. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  4. startActivity(it);  
  5. //where pkg_name is the full package path for an application  

  6. //显示指定应用的详细页面(这个好像不支持了,找不到app_id)
  7. Uri uri = Uri.parse("market://details?id=app_id");  
  8. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  9. startActivity(it);  
  10. //where app_id is the application ID, find the ID  
  11. //by clicking on your application on Market home  
  12. //page, and notice the ID from the address bar
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站      /**
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站     * 获得包安装Intent
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站     * 
@param tempFile
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站     * 
@return
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站     
*/
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站    
public   static  Intent getPackageInstallIntent(File tempFile)
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站    
{
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站        Uri mPackageURI 
= Uri.fromFile(tempFile);
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站        Intent in 
= new Intent();
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站        in.setAction(Intent.ACTION_VIEW);
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站        in.addCategory(Intent.CATEGORY_DEFAULT);
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站        in
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站                .setComponent(
new ComponentName(
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站                        
"com.android.packageinstaller",
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站                        
"com.android.packageinstaller.PackageInstallerActivity"));
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站        in.setDataAndType(mPackageURI,
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站                
"application/vnd.android.package-archive");
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站        
return in;
Android的intent - huasoft - 快乐的机器猫 小桥加加网易分站    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值