极客学院-Android基础知识

可以用来捋一捋自己所学的知识。

1, 认识 Android 中的 Activity 组件

1.1 Activity 是什么

图形界面

1.2 Activity绑定自定义视图

在protected void onCreate(){

中 setContentView(R.layout.my_layout);

}

1.3 启动另一个 Activity

定义一个Activity需要什么:

1,一个java类,继承自activity。

2,一个layout.xml 文件,定义界面。

3,在 AndroidManifest.xml 中是声明 这个activity。

 

启动一个activity。调用 startActivity(new Intent(MainActivity.this, AnotherAty.class));

好玩的,启动一个网页

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("www.baidu.com")));   //这里被懵逼的crash了,提示如下错误原因:

05-06 03:02:27.133: E/AndroidRuntime(808): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.baidu.com }
05-06 03:02:27.133: E/AndroidRuntime(808):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
05-06 03:02:27.133: E/AndroidRuntime(808):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)

一脸的懵逼,这是啥错误。原来是因为Uri.parse("www.baidu.com")  填充的有问题,正确的是如下用法:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")));

2、Activity 生命周期

本章的知识,直接参考Android的帮助文档吧,file:///D:/android-sdk/docs/reference/android/app/Activity.html (注意这是我的本地目录,仅参考)

3、在 Activity 之间传递参数

3.1、传递简单数据

 

 

5、在 Android 中 Intent 的概念及应用

5.1、显式 Intent

 

5.2、隐式 Intent

action常量的定义一般采用: 包名.intent.action.具体字符串   这个字符串是可以任意写的。

例如: org.crazyit.intent.action.CRAZYIT_ACTION     // 没错这里来自疯狂Android讲义。

 

通过隐式 intent,还有一个好好处就是,可以用来启动另一个应用的activity。这是通过显示intent无法实现的。这就好比通过淘宝启动支付宝??

如果要避免自己的应用中的activity被其它应用启动,可以在 AndroidManifest.xml中配置:

<activity android:name=”.MyAty”  android:exported=”false”>  /默认是true.就是说是可以被访问的。

 

如果无法启动另一个应用,实际上是会抛出一个异常,会导致应用crash。

我们可以捕获这个异常。

		startActivity.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View source)
			{
				//Intent intent = new Intent(Lifecycle.this
				//		, SecondActivity.class);
				//startActivity(intent);
				try{
					startActivity(new Intent("com.jikexueyuan.intent.action.myAty"));
				}catch(Exception e){
					Toast.makeText(Lifecycle.this, "无法启动指定的Activity", Toast.LENGTH_SHORT).show();
				}
			}
		});	

5.3、Intent 过滤器相关选项

如果intent - filter 配置相同的的action 和 category.则多个activity都会启动。

这个就像是说打开word, 提示你是通过 WPS, 还是内置文本阅读器打开。会提示多个匹配项。

 

5.4、通过浏览器链接启动本地 Activity

1,在本地activity中配置如下信息,Manifest中的activity添加如下filter。当然注释要删掉,不合法注释。

            <intent-filter>
                <category android:name="android.intent.category.BROWSABLE"/> //表示可以被浏览器启动
                <category android:name="android.intent.category.DEFAULT"/> //启动的是一个activity,所以肯定需要给他一个default
              
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="app" /> //表示一个协议,协议名字是app ?
            </intent-filter>

2,建立一个HTML页面,添加一个超级链接。让他通过app 协议打开某一个activity。

<body>
    <a href="app://hello">Launch My App</a>
</body>

3,在模拟器里,使用Android浏览器可以通过 10.0.2.2:port 访问本地页面。

 

6、Android 中 Context 的理解及使用

1、Context 的作用

先从Android 文档上扒下来。file:///D:/android-sdk/docs/reference/android/content/Context.html

Class Overview


Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

看的说明就知道了,Context类是用来访问应用程序的资源。访问全局信息的。图片资源,字符串资源,

 

最常用的使用

abstract Resources

getResources()

Return a Resources instance for your application's package.

Resource类的方法如下:

CharSequence

getText(int id)

Return the string value associated with a particular resource ID. The returned object will be a String if this is a plain string; it will be some other type of CharSequence if it is styled.

getResources().getText(R.string.helloworld);

tv = new TextView(this);

setText(R.string.hello_world) //具体是如何访问资源的。按F3 查看函数实现。

setText(int resid){

         setText(getContext().getResources().getText(resid));  //先获取context, 再获取Resources ,再通过资源ID获取字符串。

//而activity, service 都继承自context类。因为要访问资源。

} 

2、Application 的用途

很多情况下,我们需要多个组件之间数据的共享。我们前面已经知道Context可以实现

1, 创建一个类,让它继承 application.

import android.app.Application;

public class app extends Application{

    private String name = "";

    public boolean setText(String arg){

      this.name = arg;

      return true

}

    public String getText(){

      return name;

   }

}

2, 打开AndroidManifest.xml 进行一个配置

在<application 标签中,添加一个定义

         <application

        android:name=".App"

        android:icon="@drawable/ic_launcher"

通过这样我们就自定义了一个Application。而Application才是一个真正的全局上下文对象。

通过getApplicationContext 获取application,然后再修改。

 

 ((app)getApplicationContext()).setText("我修改了全局的内容");

 

2 Application的生命周期

Application的onCreate 函数是早于 activity 的onCreate的。而且,不论是从哪个activity启动应用,application的onCreate都会执行。

看Android文档。

void

onCreate()

Called when the application is starting, before any other application objects have been created.

void

onLowMemory()

This is called when the overall system is running low on memory, and would like actively running process to try to tighten their belt.

void

onTerminate()

This method is for use in emulated process environments.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值