Android 笔记7

来源:互联网 发布:理财成功案例知乎 编辑:程序博客网 时间:2024/06/06 22:34

目录

Android的除Activity之外的三大组件

SQLite

android 数据库的查询方法

case R.id.button_select:                //offset 要写在orderby中,如“id DESC limit 2 offset 2”,不能在limit中写limit 3 offset 2                Cursor cursor = db.query("user", null, null, null, null, null, "id DESC ", "3,2");//逗号前是偏移的数据,逗号后是显示的数据的个数//                        db.rawQuery("select * from user", null);                cursor.moveToFirst();                while (!cursor.isAfterLast()) {                    String name = cursor.getString(cursor.getColumnIndex("name"));                    String password = cursor.getString(cursor.getColumnIndex("password"));                    Log.d("user", name + ": " + password);                    cursor.moveToNext();                }                cursor.close();                break;

Content Provider

用内容提供器查询手机上的所有联系人

//用getContentResolver()方法获得实例,实例为Cursor类的对象Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                        new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, null);/*query()中的第一个参数是uri,第二个参数为指定查询的列名,3,4位约束条件,5位排序方式*/                cursor1.moveToFirst();                while (!cursor1.isAfterLast()) {                    String[] names = cursor1.getColumnNames();                    StringBuffer buffer = new StringBuffer();                    for (String name : names) {                        String value = cursor1.getString(cursor1.getColumnIndex(name));                        buffer.append("名:" + name + "  值:" + value);                    }                    Log.d("联系人", "" + buffer);                    cursor1.moveToNext();                }

Broadcast Receiver

广播的注册方式有两种,一种是在Activity中动态的注册,这时候要在onDestroy方法中取消注册,另一种是在AndroidManifest.xml中静态的注册

//在活动中发送一个广播intent = new Intent();intent.setAction("com.linj.receiver");pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0x11, intent, PendingIntent.FLAG_UPDATE_CURRENT);intent.setAction("com.linj.receiver");sendBroadcast(intent);//自定义的广播,当收到广播时弹出一个Toastpackage com.example.linj.mysqliteapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;/** * Created by Administrator on 2015/9/8. */public class MyBroadcastReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        Toast.makeText(context, "时间发生改变", Toast.LENGTH_SHORT).show();    }}

Service

//下次笔记再写

总结

作为Android的四大组件之一,无论是广播接收者,还是服务,都需要在androidmanifest中注册,有些服务还需要声明权限,Android的内容很多很庞大,老师不可能没一点都讲到,这时候就需要我们自学,自己总结。学无止境。

0 0
原创粉丝点击