zoukankan      html  css  js  c++  java
  • Android Intent笔记

    Intent由Action(动作),Data(数据),Category(分类),Type(类型),Component(组件)和Exra组成,通过这些便可以启动其他组件并传递信息。Intent类负责管理这些数据,并提供了Intent的所有方法。

    1.setComponent方法:主要用于在不同包不同应用程序间传递Intent

        工程中有两个包com.example.demointentcomponentname和com.demo.two。

        com.example.demointentcomponentname中新建一个MainActivity

        代码:

    package com.example.demointentcomponentname;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
        Button bt;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            bt = (Button)findViewById(R.id.bt);
            bt.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    ComponentName componentName = new ComponentName(MainActivity.this,"com.demo.two.FirstActivity");
                    Intent intent = new Intent();
                    intent.setComponent(componentName);
                    startActivity(intent);
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }

    xml布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        
        <Button
            android:id="@+id/bt" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="button"/>
    
    </RelativeLayout>

       com.demo.two中有一个FirsAcitivity

    package com.demo.two;
    
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import com.example.demointentcomponentname.*;
    
    public class FirstActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_first);
        }
    
        
    
    }

    xml布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".FirstActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/hello_world" />
    
    </RelativeLayout>

    博主看过有人说setComponent可以启动第三方程序的代码

    具体转载:

      

    Intent intent = new Intent();
    ComponentName comp = new ComponentName("com.tencent.android.pad",
            "com.tencent.android.pad.paranoid.desktop.DesktopActivity");
    intent.setComponent(comp);
    intent.setAction("android.intent.action.MAIN");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);    

    (博主试过了,上面是可以的)

    因为博主只用了模拟器,运行会报错,博主知识有限,希望运行成功的给个回复!

    博主也看过setComponent可以动系统程序(本例子为日历程序)

    具体代码:

    Intent intent=new Intent();   
    intent.setComponent(new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity"));   
    startActivity(intent); 

    但是博主运行时候也报错,可能是模拟器的问题!

  • 相关阅读:
    Python 递归
    Python 面向过程编程
    Python 协程函数
    Python-第三方库requests详解
    Python 三元表达式
    linux copy
    centos 安装软件
    mysql 权限
    mysql 权限 备份
    android 开发
  • 原文地址:https://www.cnblogs.com/merryjd/p/2834292.html
Copyright © 2011-2022 走看看