Intent详解

Intent是什么?以及Android的三个基本组件——Activity,Service和Broadcast Receiver——都是通过Intent机制激活的,不同类型的组件有不同的传递Intent方式,可以通过我的另一篇博客,Intent和pendingIntent机制详解里有?


这里主要讲解下Intent的用法:


示例代码一:

//定义一个Intent
Intent intent = new Intent(IntentDemo.this, AnotherActivity2.class);
//启动Activity
startActivity(intent);<strong>
</strong>

这里通过intent直接显示的调用另一个Activity的class来启动另一个Activity。


示例代码二:

//定义一个Intent
Intent intent = new Intent(Intent.ACTION_EDIT);
//启动Activity
startActivity(intent);

这里通过在Intent构造函数里加一个action,来启动一个Activity。那怎么找到这个Activity呢?


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 	package="com.halzhang.android.intent" android:versionCode="1"
	android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
         <activity android:name=".IntentDemo" android:label="@string/app_name">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
         <activity android:name=".AnotherActivity" android:label="another">
             <intent-filter>
                 <action android:name="android.intent.action.EDIT" />
                 <!-- category一定要配置,否则报错:找不到Activity -->
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
  
        <activity android:name=".AnotherActivity2" android:label="another2">
             <intent-filter>
                 <action android:name="android.intent.action.EDIT" />
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
     </application>


通过在AndroidManifest.xml里面配置声明,activity那一项的intent-filter中,增加EDIT的action,启动activity的时候就能找到这个activity启动了。

下面我们再看看其构造函数:

Intent() 
Create an empty intent. 
 
  Intent(Intent o) 
Copy constructor. 
 
  Intent(String action) 
Create an intent with a given action. 
 
  Intent(String action, Uri uri) 
Create an intent with a given action and for a given data url. 
 
  Intent(Context packageContext, Class<?> cls) 
Create an intent for a specific component. 
 
  Intent(String action, Uri uri, Context packageContext, Class<?> cls) 
Create an intent for a specific component with a specified action and data.<strong>  </strong>


最后看下Android中BatteryService中关机调用shutdownActivity的一段代码

                        Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
                        intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);//传数据
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivityAsUser(intent, UserHandle.CURRENT);

再来看下shutdownActivity的AndroidManifest.xml文件

        <activity android:name="com.android.server.ShutdownActivity"
            android:permission="android.permission.SHUTDOWN"
            android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_REQUEST_SHUTDOWN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.REBOOT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

最后看shutdownActivity的代码:

public class ShutdownActivity extends Activity {

    private static final String TAG = "ShutdownActivity";
    private boolean mReboot;
    private boolean mConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        mReboot = Intent.ACTION_REBOOT.equals(intent.getAction());//Intent的action
        mConfirm = intent.getBooleanExtra(Intent.EXTRA_KEY_CONFIRM, false);//传过来的数据
        Slog.i(TAG, "lowbattery or batteryovertempture shutdown:" + "onCreate(): confirm=" + mConfirm + " mReboot:" + mReboot);

        Thread thr = new Thread("ShutdownActivity") {
            @Override
            public void run() {
                IPowerManager pm = IPowerManager.Stub.asInterface(
                        ServiceManager.getService(Context.POWER_SERVICE));
                try {
                    if (mReboot) {
                        pm.reboot(mConfirm, null, false);
                    } else {
                        pm.shutdown(mConfirm, false);
                    }
                } catch (RemoteException e) {
                }
            }
        };
        thr.start();
        finish();
        // Wait for us to tell the power manager to shutdown.
        try {
            thr.join();
        } catch (InterruptedException e) {
        }
    }
}

比如当我们想知道是谁调用了ShutdownActivity 这个activity,我们可以从它的AndroidManifest.xml文件中的intent-filter,比如ACTION_REQUEST_SHUTDOWN,REBOOT,我们可以通过搜这两个action,看看是哪个Intent加个这两个action,再去startActivity了。






  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值