android整合--intent

在一个Android应用中可以包含零个或多个Acivity。当你的应用中包含多个Activity时,通常要在各个Activity中间跳转。在Android中,完成这些操作需要使用Intent的组件。

下面整合了一下intent的各种应用

1 如何通过intent获得另一个activity的数据

2 通过intent传递activity之间数据

3 通过intent打开手机内置应用程序

4 自定义webview


第一个activity

package com.test.one;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

public class FirstActivity extends Activity {
    /** Called when the activity is first created. */
	ProgressDialog progress = null;
	int request_code = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
    }
    
    public void onclick(View view){
    	showDialog(0);
    	progress.setProgress(0);
    	new Thread(
    			new Runnable(){
    				public void run(){
    					for(int i = 1;i <= 15;i++){
    						try {
								Thread.sleep(1000);
								progress.incrementProgressBy((int) 100/15);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
    						
    					}
    					
    					progress.dismiss();
    				
    				}
    			}
    			).start();
    	
    
    }

    public void onclick1(View view){
    	showDialog(1);
    }
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateDialog(int)
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
	switch(id){
	case 0:
		progress = new ProgressDialog(this);
		progress.setTitle("progress bar");
		progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progress.setButton(DialogInterface.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(), "you clicked the ok button", Toast.LENGTH_LONG).show();
			}
		});
		progress.setButton(DialogInterface.BUTTON_NEGATIVE, "cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(), "you clicked the cancel button", Toast.LENGTH_LONG).show();
			}
		});
          progress.setButton(DialogInterface.BUTTON_NEUTRAL, "neutral", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(), "you clicked the neutral button", Toast.LENGTH_LONG).show();
			}
		});
		return progress;
	
	case 1:
		return new AlertDialog.Builder(this)
		.setTitle("the dialog")
		.setIcon(R.drawable.icon)
		.setPositiveButton("ok", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(), "you clicked the dialog ok button", Toast.LENGTH_LONG).show();
			}
		})
		.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext(), "you clicked the dialog cancel button", Toast.LENGTH_LONG).show();
			}
		}).create();
	}
	return null;
	}
    
	
	public void onclick3(View view){
		//两种方式转到另一个activity
	//	startActivity(new Intent(this,SecondActivity.class));
	//	startActivity(new Intent("com.test.one.SecondActivity"));
		//intent传递数据方式
		Intent intent = new Intent(this,SecondActivity.class);
		intent.putExtra("str1", "this a string");
		intent.putExtra("age", 25);
		Bundle bundle = new Bundle();
		bundle.putString("str2", "this is a bundle string");
		bundle.putInt("age1", 30);
		intent.putExtras(bundle);
	    startActivityForResult(intent,request_code);
		
	}

	public void onclickweb(View view){
        //网址格式一定不要错
		 Intent i = new Intent(android.content.Intent.ACTION_VIEW,  
	                Uri.parse("http://www.huafei361.com"));  
	        startActivity(i);  
	}

//1. Uri uri = Uri.parse("tel.xxxxxx");
//2. Intent it =new Intent(Intent.ACTION_CALL,uri);
//3. 要使用这个必须在配置文件中加入<uses-permission id="android .permission.CALL_PHONE" />
	public void onclickphone(View view){
		 Intent i = new Intent(android.content.Intent.ACTION_DIAL,  
	                Uri.parse("tel:12345671234"));  
	        startActivity(i);  
	}
	
	public void onclickgeo(View view){
		 Intent i = new Intent(android.content.Intent.ACTION_VIEW,  
	                Uri.parse("geo:37.827500,-122.481670"));  
	        startActivity(i);  
	}
	public void onclickmyweb(View view){
		Intent intent = new Intent("com.test.MyBrowser");
		intent.setData(Uri.parse("http://www.huafei361.com"));
		startActivity(intent);
	}
	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(requestCode == request_code){
			if(resultCode == RESULT_OK){
				Toast.makeText(this, data.getData().toString(), Toast.LENGTH_LONG).show();
				Toast.makeText(this, data.getStringExtra("back").toString(), Toast.LENGTH_LONG).show();
			}
		}
	
	}
   
	
}

2 第二个activity

package com.test.one;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SecondActivity extends Activity {
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		Toast.makeText(this, getIntent().getStringExtra("str1")+getIntent().getIntExtra("age", 0), Toast.LENGTH_LONG).show();
		Bundle bundle = getIntent().getExtras();
		Toast.makeText(this, bundle.getString("str2")+Integer.toString(bundle.getInt("age1")), Toast.LENGTH_LONG).show();
	}
	
	public void onclick3(View view){
		Intent intent = new Intent();
		EditText edit = (EditText)findViewById(R.id.textusername);
		intent.putExtra("back", "backdata");
		intent.setData(Uri.parse(edit.getText().toString()));
		setResult(RESULT_OK,intent);
		finish();
	}
			
	
}

3 自定义的webview

package com.test.one;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class mybweb extends Activity {

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myweb);
		
		Uri uri = getIntent().getData();
		WebView myweb = (WebView)findViewById(R.id.myweb);
		myweb.setWebViewClient(new Callback());
		myweb.loadUrl(uri.toString());
	}

	private class Callback extends WebViewClient{

		/* (non-Javadoc)
		 * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
		 */
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			return false;
		}
		
	}
	
}

注意几个容易出现错误的地方:

1 intent之间传递数据一定要把数据放进intent中,否则容易产生空指针

2 intent打开手机内置应用时

  打开网页时uri的网址格式要正确,必须是http://开头

电话必须是tel:  地图必须是geo:

3 打开网页,电话要给手机权限

        <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.CALL_PHONE"/>


代码附下:http://download.csdn.net/detail/nameyuxiang/6276231

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值