Intent

Intent大致分为显示和隐式。
Intent是Android程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前足尖想要执行的动作,还可以在不同组件之间传递数据。Intent一般可被用于启动活动,启动服务以及发送广播等场景。

显示Intent:意图非常明确
创建第二个活动Second_Activity内容保持不变,布局文件命名为second_layout
内容为:




    

非主活动不用配置

笔记1.png

笔记2.png

@Override
        public void onClick(View v){
    Toast.makeText(FirstActivity.this,"you clicked Button 1", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(FirstActivity.this,SecondActivity.class);

    startActivity(intent);
}

隐式Intent:不明确指出想要启动哪个活动


    
        
        
    

笔记3.png
笔记4.png
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_layout);
    Button button1 = (Button)findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener(){
        @Override
                public void onClick(View v){
            Toast.makeText(FirstActivity.this,"you  clicked Button  1", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent("om.example.activitytest.ACTION_START");
            startActivity(intent);
            }
    });

增加一个category:

笔记5.png

在firstActivity中onClick添加
intent.addCategory("com.example.activitytest.MY_CATEGORY");
笔记6.png

中创建一个category声明:


打开网页

笔记7.png

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_layout);
    Button button2 = (Button)findViewById(R.id.Button_2);
    button2.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("http://wwww.baidu.com"));
            startActivity(intent);
        }  
    });
}

笔记8.png

创建第三个活动来响应网页的intent
在注册文件中声明


    
        
        
        
    

笔记9.png

书上是这样,但实际操作没有显示open with。.png

你可能感兴趣的:(Intent)