Intent主要负责组件之间的跳转,有两种用法:
1.显式用法:
Intent intent =new Intent(this,TargetActivity.class);
startActivity(intent);
其中,this指的是当前的.java文件,TargetActivity.class即是要跳转的页面,简而言之,显式用法就是其目标组件是确定的。
2.隐式用法:
较之显式用法,隐式用法是没有明确的目标组件的,这里运用了Action,Data,Category三个属性作为确定目标组件的方法:以下可用程序说明:
主Activity类:
package com.myreceiver.myreceiver; import sms.message.SmsSettingActivity; import com.Alarme.alarm.AlarmMainActivity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { public static final String SEND_ACTION = "com.myreceiver.myreceiver.ACTION"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.send).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.send: Intent intent = new Intent(SEND_ACTION); intent.putExtra("date", "这是chaos发的信息!"); sendBroadcast(intent); break; default: break; } } }); } }
获取信息的类:
package com.myreceiver.myreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String date=intent.getStringExtra("date"); Toast.makeText(context, date, Toast.LENGTH_LONG).show(); } }