Single

AIDE Android App基础教程_第二十课答案-Intent

第二十课,本课中您将学习如何使用intent启动应用程序的其它activity

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onGoogleButtonClick(View view)
    {
    	Uri uri = Uri.parse("http://www.google.com");
    	
    }
    
}

单击该按钮时,“onGoogleButtonClick”方法被调用

在“onGoogleButtonClick”方法里面,我们用要打开的网址创建了一个“Uri”对象

现在用代码“Intent intent = new Intent(Intent.ACTION_VIEW, uri);”创建“Intent”对象,其目的是对进行操作的抽象描述,在这个方法里面,它以一个视图intent打开一个应用程序,它可以显示一个Web地址

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onGoogleButtonClick(View view)
    {
    	Uri uri = Uri.parse("http://www.google.com");
    	Intent intent=new Intent(Intent.ACTION_VIEW,uri);
    }
    
}

调用“startActivity(intent);”。这将使Android启动一个activity,它可以处理视图intent–例如网络浏览器

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onGoogleButtonClick(View view)
    {
    	Uri uri = Uri.parse("http://www.google.com");
    	Intent intent=new Intent(Intent.ACTION_VIEW,uri);
		startActivity(intent);
    }
    
}

现在,通过更改“Uri”使按钮打开“www.android.com”

package com.aide.trainer.myapp;

import android.app.*;
import android.content.*;
import android.net.*;
import android.os.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void onGoogleButtonClick(View view)
    {
    	Uri uri = Uri.parse("http://www.android.com");
    	Intent intent=new Intent(Intent.ACTION_VIEW,uri);
		startActivity(intent);
    }
    
}

暂无评论

发表评论