一个
Intent对象 包含了一组信息:
1. Component name 指定我要启动哪一个组件
2. Action (有一系列的大写的常量)
3. Data (URI)
4. Category
5. Extras
6. Flags
来一个简单的例子:在一个主Activty里来呼叫另一个Activity
首先我们来熟悉Activity类的架构是(粗体是必须要有的)
public class Androidnew01Activity extends Activity {
private static String TAG="Androidnew01Activity";
private Button mybtn1 = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mybtn1 = (Button)this.findViewById(R.id.btn1);
mybtn1.setOnClickListener(new MyBtnListener());
}
class MyBtnListener implements Button.OnClickListener{
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "in class: MyBtnListener...");
Intent intent = new Intent(Androidnew01Activity.this,
LoginWindow.class);
startActivity(intent);
}
}
}
上面蓝色字体是启动一个新Activity的过程, 可见我们要启动的是LoginWindow这个Acitvity, 所以我们需要先:
1. 创建类 LoginWindow 继承自Activity
2. 定义一个layout的xml文件,并在类LoginWindow中以setContentView() 方式加入onCreate()中
2. 在manifest中定义LoginWindow这个 <activity>
3. 在类LoginWindow的适当时机(比如按钮或其它),通过创建和使用intent 来启动LoginWindows这个Activity
通过Intent启动一个Activity时,我们可以发现按返回键可以返回主Activty, 但是如果此时直接按Home键, 实际上并没有退出程序。
实际实验,调出LoginWindow后, 先按Home, 再从系统列表中启动这个程序,发现仍然在LoginWindow, 而不是重新从主Activty启动!!!说明刚才的Home键没有退出程序,只有一步一步通过返回键, 才能退出程序。
=================================================================
附:
uninstall apk
复制代码
install apk
复制代码
play audio
复制代码
/**
* 获得包安装Intent
* @param tempFile
* @return
*/
public
static
Intent getPackageInstallIntent(File tempFile)
{
Uri mPackageURI = Uri.fromFile(tempFile);
Intent in = new Intent();
in.setAction(Intent.ACTION_VIEW);
in.addCategory(Intent.CATEGORY_DEFAULT);
in
.setComponent(new ComponentName(
"com.android.packageinstaller",
"com.android.packageinstaller.PackageInstallerActivity"));
in.setDataAndType(mPackageURI,
"application/vnd.android.package-archive");
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return in;
}
1. Component name 指定我要启动哪一个组件
2. Action (有一系列的大写的常量)
3. Data (URI)
4. Category
5. Extras
6. Flags
来一个简单的例子:在一个主Activty里来呼叫另一个Activity
首先我们来熟悉Activity类的架构是(粗体是必须要有的)
public class Androidnew01Activity extends Activity {
private static String TAG="Androidnew01Activity";
private Button mybtn1 = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mybtn1 = (Button)this.findViewById(R.id.btn1);
mybtn1.setOnClickListener(new MyBtnListener());
}
class MyBtnListener implements Button.OnClickListener{
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "in class: MyBtnListener...");
Intent intent = new Intent(Androidnew01Activity.this,
LoginWindow.class);
startActivity(intent);
}
}
}
上面蓝色字体是启动一个新Activity的过程, 可见我们要启动的是LoginWindow这个Acitvity, 所以我们需要先:
1. 创建类 LoginWindow 继承自Activity
2. 定义一个layout的xml文件,并在类LoginWindow中以setContentView() 方式加入onCreate()中
2. 在manifest中定义LoginWindow这个 <activity>
3. 在类LoginWindow的适当时机(比如按钮或其它),通过创建和使用intent 来启动LoginWindows这个Activity
通过Intent启动一个Activity时,我们可以发现按返回键可以返回主Activty, 但是如果此时直接按Home键, 实际上并没有退出程序。
实际实验,调出LoginWindow后, 先按Home, 再从系统列表中启动这个程序,发现仍然在LoginWindow, 而不是重新从主Activty启动!!!说明刚才的Home键没有退出程序,只有一步一步通过返回键, 才能退出程序。
=================================================================
附:
Intent应该算是Android中特有的东西。你可以在Intent中指定程序要执行的动作(比如:view,edit,dial),以及程序执行到该动作时所需要的资料。都指定好后,只要调用startActivity(),Android系统会自动寻找最符合你指定要求的应用程序,并执行该程序。 下面列出几种Intent的用法 显示网页:
调用拨号程序
调用发送短信的程序
|
- Uri uninstallUri = Uri.fromParts("package", "xxx", null);
-
- returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
install apk
- Uri installUri = Uri.fromParts("package", "xxx", null);
-
- returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
play audio
- Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
-
- returnIt = new Intent(Intent.ACTION_VIEW, playUri);
- //发送附件
- Intent it = new Intent(Intent.ACTION_SEND);
- it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
- it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3");
- sendIntent.setType("audio/mp3");
- startActivity(Intent.createChooser(it, "Choose Email Client"));
- //搜索应用
- Uri uri = Uri.parse("market://search?q=pname:pkg_name");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
- //where pkg_name is the full package path for an application
-
- //显示指定应用的详细页面(这个好像不支持了,找不到app_id)
- Uri uri = Uri.parse("market://details?id=app_id");
- Intent it = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(it);
- //where app_id is the application ID, find the ID
- //by clicking on your application on Market home
- //page, and notice the ID from the address bar



















