你的位置:首页 > 操作系统

[操作系统]IntentDemo


Intent通信示例:

两个Button,一个startBrowser, 一个startPhone.

其中,OnClickListener()是类View的一个interface,需要实现其中的onClick()函数。

startActivity()开启另一个Activity,本示例中开启Browser或Phone.

Intent.ACTION_VIEW向用户display data.

 1 package com.example.shad_fnst.intentdemo; 2  3 import android.app.Activity; 4 import android.content.Intent; 5 import android.net.Uri; 6 import android.support.v7.app.ActionBarActivity; 7 import android.os.Bundle; 8 import android.view.Menu; 9 import android.view.MenuItem;10 import android.view.View;11 import android.widget.Button;12 13 14 public class MainActivity extends Activity {15 16   @Override17   protected void onCreate(Bundle savedInstanceState) {18     super.onCreate(savedInstanceState);19     setContentView(R.layout.activity_main);20 21     Button startBrowser = (Button) findViewById(R.id.btnStartBrowser);22     startBrowser.setOnClickListener(new View.OnClickListener() {23       @Override24       public void onClick(View v) {25         Intent i = new Intent(Intent.ACTION_VIEW,26             Uri.parse("http://www.baidu.com"));27         startActivity(i);28       }29     });30 31     Button startPhone = (Button) findViewById(R.id.btnStartPhone);32     startPhone.setOnClickListener(new View.OnClickListener() { //OnClickListener是一个接口33       @Override34       public void onClick(View v) {35         Intent i =new Intent(Intent.ACTION_VIEW,36             Uri.parse("tel:+8613912945369"));37         startActivity(i);38       }39     });40   }41 42   @Override43   public boolean onCreateOptionsMenu(Menu menu) {44     // Inflate the menu; this adds items to the action bar if it is present.45     getMenuInflater().inflate(R.menu.menu_main, menu);46     return true;47   }48 49   @Override50   public boolean onOptionsItemSelected(MenuItem item) {51     // Handle action bar item clicks here. The action bar will52     // automatically handle clicks on the Home/Up button, so long53     // as you specify a parent activity in AndroidManifest.54     int id = item.getItemId();55 56     //noinspection SimplifiableIfStatement57     if (id == R.id.action_settings) {58       return true;59     }60 61     return super.onOptionsItemSelected(item);62   }63 }

MainActivity.java
 1 <??> 2 <manifest ="http://schemas.android.com/apk/res/android" 3   package="com.example.shad_fnst.intentdemo" > 4  5   <application 6     android:allowBackup="true" 7     android:icon="@mipmap/ic_launcher" 8     android:label="@string/app_name" 9     android:theme="@style/AppTheme" >10     <activity11       android:name=".MainActivity"12       android:label="@string/app_name" >13       <intent-filter>14         <action android:name="android.intent.action.MAIN" />15 16         <category android:name="android.intent.category.LAUNCHER" />17       </intent-filter>18     </activity>19   </application>20 21 </manifest>

AndroidMainfest.
 1 <LinearLayout ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:paddingLeft="@dimen/activity_horizontal_margin" 6   android:paddingRight="@dimen/activity_horizontal_margin" 7   android:paddingTop="@dimen/activity_vertical_margin" 8   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 9   android:orientation="vertical">10 11   <Button12     android:layout_width="fill_parent"13     android:layout_height="wrap_content"14     android:id="@+id/btnStartBrowser"15     android:text="@string/start_browser"/>16   <Button17     android:layout_width="fill_parent"18     android:layout_height="wrap_content"19     android:id="@+id/btnStartPhone"20     android:text="@string/start_phone"/>21 22 </LinearLayout>

activity_main.
1 <resources>2   <string name="app_name">IntentDemo</string>3 4   <string name="hello_world">Hello world!</string>5   <string name="action_settings">Settings</string>6   <string name="start_browser">Start Browser</string>7   <string name="start_phone">Start Phone</string>8 </resources>

strings.