Intent中可以直接使用Intent.setClass(),也可以使用组件Component.
简单的使用方式如下:
1 package com.example.test; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.ComponentName; 6 import android.content.Intent; 7 import android.view.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class MainActivity extends Activity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 Button btn = (Button) findViewById(R.id.btn); 19 btn.setOnClickListener(new OnClickListener() { 20 21 @Override 22 public void onClick(View v) { 23 // TODO Auto-generated method stub 24 Intent intent = new Intent(); 25 //1.利用setClass 26 //intent.setClass(MainActivity.this, SencondActivity.class); 27 //2.利用setComponent ComponentName的第一个参数必须是application‘s package name,也是主配置文件Manifest里设置好的包名 28 ComponentName name = new ComponentName(getPackageName(), SencondActivity.class.getName()); 29 intent.setComponent(name); 30 startActivity(intent); 31 } 32 }); 33 } 34 35 @Override 36 public boolean onCreateOptionsMenu(Menu menu) { 37 // Inflate the menu; this adds items to the action bar if it is present. 38 getMenuInflater().inflate(R.menu.activity_main, menu); 39 return true; 40 } 41 42 } 43 44
时间: 2024-10-18 23:40:45