码迷,mamicode.com
首页 > 移动开发 > 详细

Android 匿名启动activity 启动系统activity

时间:2015-05-17 16:47:18      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:android   app   

一般我们使用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);
	}

}



Android 匿名启动activity 启动系统activity

标签:android   app   

原文地址:http://blog.csdn.net/u010840444/article/details/45789169

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!