Android 匿名启动activity 启动系统activity

一般我们使用Intent 进行activity跳转时我们都知道需要跳转的activity的名字,例如:

				Intent intent=new Intent(FirstActivity.this,SecondActitivy.class);
				startActivity(intent);
SecondActitivy.class和FirstActivity不再同一个App的时候,我们就需要用到匿名启动,

匿名启动:

首先需要设置被启动的SecondActivity 的xml配置文件:

   <activity android:name="testIntent.SecondActitivy" >
            <intent-filter>
                <action android:name="toSecondPage" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
FirstActivity 可以利用 <action android:name="toSecondPage" />

来找到 SecondActivity

FirstActivity代码如下:

<span style="white-space:pre">				</span>Intent intent=new Intent();
				intent.setAction("toSecondPage");
				startActivity(intent);


这样就可以利用ActionName调到非本App的Activity


启动系统activity也是这样实现:

通过一个系统提供的ActionName来跳转到系统的Activity,同时可以附加一些消息;(xml 就是4个简单的按钮)

package testIntent;

import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.androidtest.R;

public class FirstActivity extends Activity implements OnClickListener{
	
	private Button toWeBButton;
	private Button toPicButton;
	private Button toMesButton;
	private Button toPhoneButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first_activity_main);
	
		toWeBButton=(Button) findViewById(R.id.toWeb);
		toPicButton=(Button) findViewById(R.id.toPic);
		toMesButton=(Button) findViewById(R.id.toMes);
		toPhoneButton=(Button) findViewById(R.id.toPhone);
		
		toWeBButton.setOnClickListener(this);
		toPicButton.setOnClickListener(this);
		toMesButton.setOnClickListener(this);
		toPhoneButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent=new Intent();
		
		if(v.equals(toWeBButton)){//跳转到 网页 百度首页
			intent.setAction(Intent.ACTION_VIEW);
			Uri uri=Uri.parse("http://www.baidu.com");
			intent.setData(uri);
		}else if(v.equals(toPicButton)){//打开系统图片
			intent.setAction(Intent.ACTION_GET_CONTENT);
			intent.setType("image/*");//打开所有的图片, 如果需要获取图片就需要写回调函数
		}else if(v.equals(toMesButton)){//发送消息
			intent.setAction(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_TEXT, "这是我第一次这样发信息");
			
		}else if(v.equals(toPhoneButton)){//拨打电话
			intent.setAction(Intent.ACTION_VIEW);
			Uri uri=Uri.parse("tel:1839860592");
			intent.setData(uri);
		}
		startActivity(intent);
	}

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值