安卓重点知识

in 网站建设
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

1.数据库读写

首先新建一个数据库工具类继承自SQLiteOpenHelper,然后在构造方法中指定数据库名称

和版本号进行初始化。代码如下:

MyDataBase.java

package chzu.csci.pwn.androidquiz;

import android.content.ContentValues;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class MyDataBase extends SQLiteOpenHelper {

    private static final String DB_NAME="2017211808.db";

    private static final int DB_VERSION=1;

    MyDataBase(Context context){

        super(context,DB_NAME,null,DB_VERSION);

    }

    @Override

    public void onCreate(SQLiteDatabase db) {

        db.execSQL(//第一次运行时新建一个BANK表,并且插入初始数据

                "CREATE TABLE BANK (_id INTEGER PRIMARY KEY AUTOINCREMENT,"

                        +"TYPE TEXT,"

                        +"QUESTION TEXT,"

                        +"ANSWER TEXT);"

        );

        insertBank(db,"架构","Android系统架构自底向上第二层是?");

        insertBank(db,"编程","Android App开发支持编程语言包括?");

        insertBank(db,"服务","绑定服务使用什么方法?");

    }

    @Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    private static void insertBank(SQLiteDatabase db,String type,String question){

        ContentValues value=new ContentValues();

        value.put("TYPE",type);

        value.put("QUESTION",question);

        long result=db.insert("BANK",null,value);

    }

}

1.插入数据

调用SQLiteDatabase类的insert方法即可,代码如下,具体调用可以看上面的MyDataBase.java:

ContentValues value=new ContentValues();

value.put("TYPE",type);//字段名和值

value.put("QUESTION",question);//字段名和值

long result=db.insert("BANK",null,value);

2.删除数据

调用SQLiteDatabase类的delete方法即可,代码如下:

SQLiteOpenHelper helper=new MyDataBase(this);

try{

     SQLiteDatabase db=helper.getWritableDatabase();

     //删除TYPE字段为"架构"的行

function(){ //外汇经纪商动态 www.fx61.com/news

     db.delete("BANK","TYPE=?",new String[]{"架构"});

 }catch (SQLException e){

     Toast.makeText(this,"DataBase unavalibale",Toast.LENGTH_SHORT).show();

 }

3.修改数据

调用SQLiteDatabase类的update方法,传入ContentValues对象即可,代码如下:

SQLiteDatabase db=helper.getWritableDatabase();

ContentValues bankvalues=new ContentValues();

bankvalues.put("ANSWER",answer);//ANSWER为想要修改的字段名,answer为想要修改成的值

db.update("BANK",bankvalues,"_id=?",new String[]{Integer.toString(ID)});

4.查询数据

调用SQLiteDatabase类的query方法,返回一个Cursor对象,类似于ResultSet结果集,

代码如下:

SQLiteDatabase db=helper.getReadableDatabase();

cursor= db.query("BANK",new String[]{"_id","TYPE"},null,null,null,null,null);//相当于执行SQL语句SELECT _id,TYPE FROM BANK

加上查询条件,代码如下:

SQLiteDatabase db=helper.getReadableDatabase();

cursor=db.query("BANK",new String[]{"QUESTION","ANSWER"},"_id=?",

         new String[]{Integer.toString(ID)},null,null,null);

                 

2.按键事件处理

public void onClickSave(View view){

      

}

3.CursorAdapter(游标适配器)

通过findViewById方法获取ListView组件,然后再调用db的query方法获取数据库中的内容,

返回值是一个Cursor游标对象,对SimpleCursorAdapter类进行构造后生成对象后,调用

ListView对象的setAdapter方法,进行适配器设置,即可在页面中显示数据库内容。

SimpleCursorAdapter listAdapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,

                    cursor,new String[]{"TYPE"},new int[]{android.R.id.text1},0);

listView.setAdapter(listAdapter);

4.界面跳转时传递数据

通过构造一个Intent对象,然后调用intent对象的putExtra方法存入数据,最后调

用startActivity方法启动。

Intent intent=new Intent(MainActivity.this,SecondActivity.class);

intent.putExtra("ID",(int)id);

startActivity(intent);

5.Toast会话

Toast.makeText(this,"DataBase unavalibale",Toast.LENGTH_SHORT).show();

6.音乐播放服务

通过绑定式服务即可实现,在res目录下新建一个raw目录,然后将音乐文件粘贴进该目录,具体代码如下:

MusicService.java

package chzu.csci.pwn.androidquiz;

import android.app.Service;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

import androidx.annotation.Nullable;

public class MusicService extends Service{

    private MediaPlayer mediaPlayer;

    //Service初始化时调用

    public void onCreate(){

        super.onCreate();

        mediaPlayer = MediaPlayer.create(this, R.raw.notice);//这里注意将音乐文件修改成自己的

    }

    public class MusicBinder extends Binder{

        MusicService getMusic(){

            return MusicService.this;

        }

    }

    private IBinder binder=new MusicBinder();

    @Override

    public IBinder onBind(Intent intent) {

        if(!mediaPlayer.isPlaying()){

            // 开始播放

            mediaPlayer.start();

            // 允许循环播放

            mediaPlayer.setLooping(true);

        }

        return binder;

    }

    public boolean onUnbind(Intent intent) {

        if(mediaPlayer.isPlaying()){

            mediaPlayer.stop();

        }

        return super.onUnbind(intent);

    }

    @Override

    public void onDestroy() {

        // TODO Auto-generated method stub

        super.onDestroy();

        //先停止 再释放

        if(mediaPlayer.isPlaying()){

            mediaPlayer.stop();

        }

        mediaPlayer.release();

    }

    public MusicService() {

    }

}

onClickStart方法调用后播放音乐,onClickStop方法调用后停止播放,可以将方法与按钮的onClick进行绑定,从而通过按钮控制音乐开关。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    

    private MusicService musicService;

    private ServiceConnection connection;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       

        connection=new ServiceConnection() {

            @Override

            public void onServiceConnected(ComponentName name, IBinder service) {

                MusicService.MusicBinder binder=(MusicService.MusicBinder)service;

                musicService=binder.getMusic();

            }

            @Override

            public void onServiceDisconnected(ComponentName name) { }

        };

       

    }

    

    public void onClickStart(View view){

       

        Intent intent = new Intent(this,MusicService.class);

        bindService(intent,connection,BIND_AUTO_CREATE);

    }

    public void onClickStop(View view){

      

       unbindService(connection);     

    }

}

7.横竖屏切换处理

主要就是将Activity的某些变量在Activity退出调用onSaveInstanceState时进行值储存,然后在载入Activity调用onCreate方法时进行值读取,相关代码如下:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private boolean running=false;

    private int seconds=0;

    private boolean wasRunning=false;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if(savedInstanceState!=null){

            seconds=savedInstanceState.getInt("seconds");

            running=savedInstanceState.getBoolean("running");

            wasRunning=savedInstanceState.getBoolean("wasRunning");

        }

    }

    @Override

    protected void onStop() {

        super.onStop();

        wasRunning=running;

        running=false;

    }

    @Override

    protected void onStart() {

        super.onStart();

        if(wasRunning){

            running=true;

        }

    }

    @Override

    public void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);

        outState.putBoolean("running",running);

        outState.putInt("seconds",seconds);

        outState.putBoolean("running",wasRunning);

    }

}

8.线程

一些耗时操作可以用线程操作,以免堵塞。可以声明一个内部类继承自AsyncTask,然后

重写其onPreExecute、doInBackground、onPostExecute方法。调用时调用该内部类对象

的execute方法即可,注意drinkId与AsyncTask<Integer,Void,Boolean>中的Integer相对应。

public class DrinkActivity extends AppCompatActivity {

    public void onFavoriteClicked(View view){

        int drinkId=getIntent().getIntExtra(EXTRA_DRINKID,0);

        new UpdateDrinkTask().execute(drinkId);

    }

    private class UpdateDrinkTask extends AsyncTask<Integer,Void,Boolean>{

        private ContentValues drinkvalues;

        @Override

        protected void onPreExecute() {

            CheckBox favorite=(CheckBox)findViewById(R.id.favorite);

            drinkvalues=new ContentValues();

            drinkvalues.put("FAVORITE",favorite.isChecked());

        }

        @Override

        protected Boolean doInBackground(Integer... integers) {

            

            int drinkId=getIntent().getIntExtra(EXTRA_DRINKID,0);

            SQLiteOpenHelper helper=new StarbuzzDatabaseHelper(DrinkActivity.this);

            try{

                SQLiteDatabase db=helper.getWritableDatabase();

                db.update("DRINK",drinkvalues,"_id=?",new String[]{Integer.toString(drinkId)});

                return true;

            }catch (SQLException e){

                return false;

            }

        }

        @Override

        protected void onPostExecute(Boolean aBoolean) {

           

        }

    }

}

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看