intent對於電話和瀏覽器調用,intent瀏覽器調用
1、建立xml檔案及按鈕
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/intentbtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="intent呼叫瀏覽器"
android:layout_marginTop="100dp"/>
<Button
android:id="@+id/intentbtncall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="intent調用電話"/>
</LinearLayout>
2、新建立一個Intent的java檔案
ublic class Intent_s extends Activity {
//定義兩個屬性
private Button mbutton,callbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
//初始化屬性
mbutton = (Button) findViewById(R.id.intentbtn);
callbutton=(Button)findViewById(R.id.intentbtncall);
//監聽intent調用電話的按鈕
callbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//標示地址
Uri uri = Uri.parse("tel:13881715535");
//新建立intent調用
Intent it = new Intent(Intent.ACTION_DIAL, uri);
//啟動
startActivity(it);
}
});
//監聽intent呼叫瀏覽器的監聽
mbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//標示地址
Uri uri = Uri.parse("http://www.baidu.com");
//新建立intent
Intent intent = new Intent();
//調用intent
intent.setAction(Intent.ACTION_VIEW);
//向intent放入資料
intent.setData(uri);
//啟動
startActivity(intent);
}
});
}