Intent这个东西
Intent这个东西
?
?
Intent学名:意图,主要用于android应用的各个组件之间的通讯。它包含发生请求的组件中的一组信息送给被请求的对象。你可以想象它是快递员,它携带了各中“物件“给客户。
要寄快递,我们得先打电话。拿起你的Android手机,按下拨号键盘的时候你的android手机做了些什么?“意图“有用吗?
?
按下按键这个动作后
public void dialButtonPressed() { if (isDigitsEmpty()) { // No number entered. handleDialButtonClickWithEmptyDigits(); } else { final String number = mDigits.getText().toString(); // "persist.radio.otaspdial" is a temporary hack needed for one carrier's automated // test equipment. // TODO: clean it up. if (number != null && !TextUtils.isEmpty(mProhibitedPhoneNumberRegexp) && number.matches(mProhibitedPhoneNumberRegexp) && (SystemProperties.getInt("persist.radio.otaspdial", 0) != 1)) { Log.i(TAG, "The phone number is prohibited explicitly by a rule."); if (getActivity() != null) { DialogFragment dialogFragment = ErrorDialogFragment.newInstance( R.string.dialog_phone_call_prohibited_message); dialogFragment.show(getFragmentManager(), "phone_prohibited_dialog"); } // Clear the digits just in case. mDigits.getText().clear(); } else {// 省略前面100字,看这里已经叫到“快递员“Intent了,快递员拿着电话号码“number“ 去送给DialtactsActivity.EXTRA_CALL_ORIGIN这个家伙 final Intent intent = ContactsUtils.getCallIntent(number, /*s1*/ (getActivity() instanceof DialtactsActivity ? ((DialtactsActivity)getActivity()).getCallOrigin() : null)); startActivity(intent); mClearDigitsOnStop = true; getActivity().finish(); } } } public static Intent getCallIntent(String number, String callOrigin) {/*s2*/ return getCallIntent(getCallUri(number), callOrigin); } public static Intent getCallIntent(Uri uri, String callOrigin) {/*s3*/ final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, uri); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (callOrigin != null) { intent.putExtra(DialtactsActivity.EXTRA_CALL_ORIGIN, callOrigin); } return intent; }
?
完成了一次送递!
哦!Intent原来就是这样用。温故而之新,这也是启动Activity的例子。其实Intent不但可以启动Activity,他还可以启动Service 、Broadcast 这些组建。
小结一下:
intnet 勾搭了 activity ?方式:startActivity() ,startActivityForResult()
intnet 勾搭了 service ? 方式:startService() ,bindService()
intnet 勾搭了 broadcast 方式: sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast()