显示意图 隐式意图

页面1

package com.bw.second;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//启动secondActivity
public void click(View view){
//隐式意图:应用场景:用于激活别的用用程序的activity
//打人,打酱油
Intent intent=new Intent();
//设置行为为
intent.setAction("com.bw.second.Second");//任意内容的字符串
intent.addCategory("android.intent.category.DEFAULT");
//数据 
intent.setData(Uri.parse("http://www.baidu.com:90/aaa"));
startActivity(intent);
}

//显式意图

public void click2(View view){
Intent intent=new Intent();
//第一中写法,常用写法
//intent.setClass(this, SecondActivity.class);
//2种写法
intent.setClassName(this, "com.bw.second.SecondActivity");
intent.setData(Uri.parse("http://www.baidu.com:90/aaa"));
startActivity(intent);

}

public void openCamera(View view){
Intent intent=new Intent();
//intent.setAction(android.media.action.I);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

 

页面2

package com.bw.second;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class SecondActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

//getIntent().getData().getHost();
//getIntent()获取启动当前activity对象的意图
Intent intent=getIntent();
Uri uri= intent.getData();//获取意图传过来的数据
//uri.getScheme():获取前缀
System.out.println("前缀是:"+uri.getScheme());
System.out.println("主机是:"+uri.getHost());
System.out.println("路径是:"+uri.getPath());
System.out.println("端口是:"+uri.getPort());

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

posted @ 2017-10-08 20:26  安琪儿。流苏  阅读(49)  评论(0)    收藏  举报