现在的位置: 首页 > 综合 > 正文

android学习小笔记

2014年02月01日 ⁄ 综合 ⁄ 共 6687字 ⁄ 字号 评论关闭

积累~
下面是用于备忘的分散的小知识点,未正式归类和详细分析

1.simpleAdapter
只可以用来放三种view,包括 实现了Checkable的view(例如checkBox,他绑定的值是一个布尔类型),TextView(他绑定的值是一个String,在bindView里他会执行setViewTest设置该TextView的显示的Text为该String),ImageView(他可以绑定一个图像的Rsid,也可以绑定一个String,在bindView里他会执行setViewImage)。
其构造函数SimpleAdapter(Context context, List<? extends Map<String, ?>> data,  int resource, String[] from, int[] to) ,需要构造包含map的list的数据,在from和to中指明各个item该如何绑定,resource指定layout。   这种adapter只适用于简单的图片 文本 单项或多选按钮 设置,要想使用更高级复杂的设置,该使用baseAdapter。

2.ViewFlipper与ViewSwitcher
都支持切换动画,实质上继承自ViewAnimator。他们的布局跟frameLayout相似,因为ViewAnimator继承自frameLayout, 但是他们是一个封装好的支持动画的frameLayout,可以随意设置淡入淡出切换动画,在xml中使用 android:in(out)Animation ,在代码中使用  setIn(out)Animation。还有showNext和showPrevious都是一些常用的方法。

3.overridePendingTransition
在startActivity(intent) 和finish时被调用 ,用来设置开场的activity进入动画和 退出动画 ,overridePendingTransition(int enterAnim, int exitAnim)第一个参数是进场动画的resid,第二个是退出动画的resid

3.pendingIntent和intent的区别

Java代码
  1. Notification n = new Notification(R.drawable.face_1, "Service启动", System.currentTimeMillis());  
  2. PendingIntent contentIntent = PendingIntent.getActivity(this0new Intent(this,
    TServiceHolder.class), 0);  
  3. n.setLatestEventInfo(this"任务标题""任务内容",
    contentIntent);  
  4. nManager.notify(NOTIFICATION_ID, n); // 任务栏启动  

PendingIntent和Intent的区别:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.
intent是用来立即响应使用的,用来跳转的桥梁。而pendingIntent是一个待定的intent,当远程服务,activity或者广播或app接收到pendingIntent时可以通过intent看到发送者的意图,再决定用于拦截传递或执行。pendingIntent一般只用于短信,通知,闹钟等

1. GSM网络中android发送短信示例

(1)代码节选

Java代码
  1. String msg ="你好,美女";  
  2. String number = "135****6784";  
  3. SmsManager sms = SmsManager.getDefault();  
  4.   
  5. PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(...),0);  
  6. sms.sendTextMessage(number, null, msg, pi, null);  
  7. Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();  

(2)代码解释

      PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified
as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去 。

      函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:

      1)PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;

      2)PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的PendingIntent广播。

      查看PendingIntent 类可以看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操作。

总结:

Intent 表示一个目的,第一个参数表示所在类,第二个参数表示目标类
PendingIntent 即是一个Intent的描述
PendingIntent和Intent的区别:
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情
换种说法Intent 字面意思是意图,即我们的目的,我们想要做的事情,在activity中,我们可以立即执行它
PendingIntent 相当于对intent执行了包装,我们不一定一定要马上执行它,我们将其包装后,传递给其他activity或application
这时,获取到PendingIntent  的application 能够根据里面的intent 来得知发出者的意图,选择拦击或者继续传递或者执行.

AppWidget
桌面小组件
HTC Sense 天气
墨迹天气
音乐播放器
时钟

功能:在桌面显示信息,不需要进入应用
      同时可以刷新widget的数据
      
好处:方便,便于查看信息    

自定义AppWidget步骤:
1) 创建Android XML文件,选择Appwidget provider
2) 填充配置项
   其中minwidth的计算公式:74 * 2 - 2
   
3) 创建一个用来显示appwidget的布局   
   analogclock.xml
4) appwidget配置项的initiallayout配置为analogclock.xml
5) 在清单文件中配置:
    <receiver android:name="">
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/analogwidget" />
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
    </receiver>
6) 创建一个类,继承AppWidgetProvider
   覆写:
   onEnabled()
   onDisabled()
   onUpdate()
   onDeleted()
   onReceive()  
   
   执行顺序:
   创建: onEnable  onUpdate
   删除: onDelete  onDisable
     
7) 配置receiver的name    

刷新appwidget
1) onUpdate
2) onReceive

交互

AppWidget配置Activity
1) appwidget配置项:
   android:configure="org.yuchen.appwidget.EX122_appwidgetActivity">
   注意:完整的包名+类名
2) 在清单中配置过滤器
   <intent-filter>
      <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
   </intent-filter>
3) 在配置Activity中
   在finish()之前,调用
   setResult(RESULT_OK, resultValue);
   
   注意:加一个无参数构造方法
   super();
 
注意:桌面小组件支持的View,
      类的注释必须有@RemoteView


6.SQLiteOpenHelper

抽象类,用来创建数据库SQLiteDatabase的帮助类,必须重写onCreate和onUpgrade方法,使用getWritableDatabase和getReadableDatabase得到数据库。而实际上对数据库数据的操作,还是要用SQLiteDatabase的方法如query(),close(),update(),delete()等。

7.android中的线程是如何交互的
需要用到looper和handler。 记住:一个线程对应一个looper ,looper是用来产生循环队列的,handler是用来发送消息和处理消息的

class LooperThread extends Thread

{

public Handler mHandler;

public void run() 

{

Looper.prepare();

mHandler = new Handler() 

{

public void handleMessage(Message msg) 

{

// process incoming messages here

}

};

Looper.loop();

}

线程与线程间的交互是通过消息机制,通过handler来传递消息,looper来循环读取消息并交给handler处理。
注意:
一,UI线程在创建的时候就建立了消息循环(在ActivityThread的public static final void main(String[] args)方法中实现),因此我们可以在其他线程给UI线程的handler发送消息,达到更新UI的目的。 所以如果你在ui线程处理消息(handleMessage()),就不需要创建looper,因为上述main方法中Looper.mylooper()已经为ui线程创建一个循环序列。
二,如果你在ui线程发送消息(sendMessage)给其他线程,让其他线程(work_thread)处理消息(handleMessage),那么你必须先在其他线程创建消息队列prepare,然后创建handler,并将它变为成员变量好让主线程使用它来obtainMessage

public class LooperThreadActivity extends Activity{  
    /** Called when the activity is first created. */  
      
    private final int MSG_HELLO = 0;  
    private Handler mHandler;  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        new CustomThread().start();//新建并启动CustomThread实例  
          
        findViewById(R.id.send_btn).setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {//点击界面时发送消息  
                String str = "hello";  
                Log.d("Test", "MainThread is ready to send msg:" + str);  
                mHandler.obtainMessage(MSG_HELLO, str).sendToTarget();//发送消息到CustomThread实例  
                  
            }  
        });  
          
    }  
     
      
    class CustomThread extends Thread {  
        @Override  
        public void run() {  
            //建立消息循环的步骤  
            Looper.prepare();//1、初始化Looper  
            mHandler = new Handler(){//2、绑定handler到CustomThread实例的Looper对象  
                public void handleMessage (Message msg) {//3、定义处理消息的方法  
                    switch(msg.what) {  
                    case MSG_HELLO:  
                        Log.d("Test", "CustomThread receive msg:" + (String) msg.obj);  
                    }  
                }  
            };  
            Looper.loop();//4、启动消息循环  
        }  
    }  
}  

8.listView.setSelection(position);
setSelection()方法在touch模式(即触碰模式)下,listview的某一项不会被选中,但是会被positioned,getSelectedItemPositon是必须在选中的状态下才返回值得(他默认返回-1)。
如果硬是要设置选中项的背景颜色,可以使用成员变量记录选中的位置,然后再setAdapter一次,在getview中监控选中的位置是否等于方法参数position,从而设置他的backgroundColor。
setSelection(positon)方法的实际作用是控制position能在listView的顶层显示。

9.scrollview.scrollTo(selected*20, 0);
scrollto方法里传递的两个参数是x y的坐标。以左上角为(0,0),并不是scrollview里面的linerlayout里的第几个view的意思。所以,需要计算每个view的宽高,然后用宽or高去乘以 位置(position)就能得到准确位置。区分scrollBy(int, int),这个是“移动了”,前者为“移动到”。

10.listView.smoothscrolltoPosition(int)
可以使listview平滑滚动到某一项,而setSelection同样可以指定显示那一项,但是没有滚动效果。

11.隐藏输入面板
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(PreferenceActivity.this
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);

12.关闭activity
第一种,调用finish(),第二种使用startActivityforResult()启动另一个activity,在调用finishActivity()去关闭之前启动过的activity

【上篇】
【下篇】

抱歉!评论已关闭.