Intent理论介绍: http://www.cnblogs.com/skynet/archive/2010/07/20/1781644.html
自定义Action
1. 建一个HelloWord 类似的project (名为 ActionTest)
修改ActionTestManifest.xml 为:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.ActionTest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ActionTestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="test_action" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
2. 建一个可以相应一个button的project, 把其中到按键相应函数修改为:
mButton1 = (Button) findViewById(R.id.Button1);
mButton1.setText("Button");
//mButton1.setWidth(100);
//mButton1.setHeight(50);
mButton1.setTextColor(Clolor.GREEN);
mButton1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Toast toast = Toast.makeText(IDOdestopActivity.this,"you press button " +
mButton1.getText() ,Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 100);
toast.show();
Intent In = new Intent();
In.setAction("test_action");
startActivity(In);
}
});
这样后点击button就会跑程序ActionTest。
应用2 : 打开PDF文件
private void OpenGuidPdf()
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File("/data/uifinajuly20.pdf"));
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}