1、广播的两种注册方法,有什么区别
public class BroadcastReceiverTest extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//拿到我们注册的广播类型
String action = intent.getAction();
Log.d("TAG",action);
//TODO 处理自己逻辑...
}
}
<receiver android:name=".BroadcastReceiverTest">
<!-- 订阅我们这个广播接收者感兴趣的广播类型 -->
<intent-filter >
<action android:name="com.scu.lly.action.MyBROADCAST"/>
<action android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<receiver android:name=".BroadcastReceiverTest">
<!-- 订阅我们这个广播接收者感兴趣的广播类型 -->
<intent-filter >
<!-- 这是我们自定义的广播类型 -->
<action android:name="com.scu.lly.action.MyBROADCAST"/>
<!-- 这是我们系统的开机广播 -->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
public class BroadcastReceiverTest extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//拿到我们注册的广播类型
String action = intent.getAction();
Log.d("TAG",action);
if("com.scu.lly.action.MyBROADCAST".equals(action)){//处理我们接收到的自定义的广播
//TODO
}else if("android.intent.action.BOOT_COMPLETED".equals(action)){//处理开机广播
//TODO
}
}
}
BroadcastReceiverTest receiver = new BroadcastReceiverTest();
IntentFilter filter = new IntentFilter();
filter.addAction("com.scu.lly.action.MyBROADCAST");
registerReceiver(receiver, filter);
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
Intent intent = new Intent("com.scu.lly.action.MyBROADCAST");//发送指定类型的广播
intent.putExtra("name", "lly");//在发送广播的同时,还可以携带一些参数过去
sendBroadcast(intent);
public class BroadcastReceiverTest extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//拿到我们注册的广播类型
String action = intent.getAction();
Log.d("TAG",action);
if("com.scu.lly.action.MyBROADCAST".equals(action)){//处理我们接收到的自定义的广播
//往下一级传递时,将这里的处理结果传过去
Bundle bundle = new Bundle();
bundle.putString("result", "resultinfo");
setResultExtras(bundle);
// abortBroadcast();
}
}
}
public class BroadcastReceiverTest2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//拿到我们注册的广播类型
String action = intent.getAction();
Log.d("TAG",action);
if("com.scu.lly.action.MyBROADCAST".equals(action)){//处理我们接收到的自定义的广播
//拿到上一级接收者的处理结果
Bundle bundle = getResultExtras(true);
String res = bundle.getString("result");
Log.d("TAG", res);
}
}
}
<receiver android:name=".BroadcastReceiverTest">
<intent-filter android:priority="100">
<!-- 这是我们自定义的广播类型 -->
<action android:name="com.scu.lly.action.MyBROADCAST"/>
<action android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
<receiver android:name=".BroadcastReceiverTest2">
<intent-filter android:priority="90">
<!-- 这是我们自定义的广播类型 -->
<action android:name="com.scu.lly.action.MyBROADCAST"/>
<action android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
Intent intent = new Intent("com.scu.lly.action.MyBROADCAST");
intent.putExtra("name", "lly");//在发送广播的同时,还可以携带一些参数过去
sendOrderedBroadcast(intent, "com.scu.lly.permission.MyBROADCAST_PERMISSION");
2、Android数据缓存的五种方法
SharedPreferences settings = context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE);//第一步
SharedPreferences.Editor editor = settings.edit();//第二步
editor.putString("name", "lly");//第三步
editor.commit();//第四步
Context.MODE_PRIVATE: 指定该SharedPreferences数据只能被本应用程序读、写。
Context.MODE_WORLD_READABLE: 指定该SharedPreferences数据能被其他应用程序读,但不能写。
Context.MODE_WORLD_WRITEABLE: 指定该SharedPreferences数据能被其他应用程序读,写
第二步,通过 SharedPreferences 获取Editor/**
* SharedPreferences参数管理类
*/
public class PrefsUtils {
// 本APP保存SharedPreferences参数的文件名
private static final String APP_Prefs_Name = "roadheadline";
/**
* 保存字符串
*
* @param context
* @param key
* @param value
* @return 保存成功返回true
*/
public static boolean putString(Context context, String key, String value) {
return context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE).edit().putString(key, value).commit();
}
/**
* 获取字符串
*
* @param context
* @param key
* @return 若不存在该key值对应的value,返回null
*/
public static String getString(Context context, String key) {
return getString(context, key, null);
}
/**
* 获取字符串
*
* @param context
* @param key
* @param defValue
* @return 若不存在该key值对应的value,返回用户传入的defValue
*/
public static String getString(Context context, String key, String defValue) {
return context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE).getString(key, defValue);
}
/**
* 保存int数据
*
* @param context
* @param key
* @param value
* @return 保存成功返回true
*/
public static boolean putInt(Context context, String key, int value) {
SharedPreferences settings = context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(key, value);
return editor.commit();
}
/**
* 获取int数据
* @param context
* @param key
* @return 不存在返回-1
*/
public static int getInt(Context context, String key) {
return getInt(context, key, -1);
}
/**
* 获取int数据
*
* @param context
* @param key
* @param defaultValue
* @return
*/
public static int getInt(Context context, String key, int defaultValue) {
SharedPreferences settings = context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE);
return settings.getInt(key, defaultValue);
}
/**
* 保存long类型数据
*
* @param context
* @param key
* @param value
* @return
*/
public static boolean putLong(Context context, String key, long value) {
SharedPreferences settings = context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putLong(key, value);
return editor.commit();
}
/**
* 获取long类型数据
*
* @param context
* @param key The name of the preference to retrieve
* @return 不存在返回-1
*/
public static long getLong(Context context, String key) {
return getLong(context, key, -1);
}
/**
* 获取long类型数据
*
* @param context
* @param key
* @param defaultValue
* @return
*/
public static long getLong(Context context, String key, long defaultValue) {
SharedPreferences settings = context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE);
return settings.getLong(key, defaultValue);
}
/**
* 保存float类型数据
*
* @param context
* @param key
* @param value
* @return
*/
public static boolean putFloat(Context context, String key, float value) {
SharedPreferences settings = context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putFloat(key, value);
return editor.commit();
}
/**
* 获取float类型数据
*
* @param context
* @param key
* @return 不存在返回-1
*/
public static float getFloat(Context context, String key) {
return getFloat(context, key, -1);
}
/**
* 获取float类型数据
*
* @param context
* @param key
* @param defaultValue
* @return
*/
public static float getFloat(Context context, String key, float defaultValue) {
SharedPreferences settings = context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE);
return settings.getFloat(key, defaultValue);
}
/**
* 保存boolean类型数据
*
* @param context
* @param key
* @param value
* @return
*/
public static boolean putBoolean(Context context, String key, boolean value) {
SharedPreferences settings = context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
return editor.commit();
}
/**
* 获取boolean类型数据
*
* @param context
* @param key
* @return 不存在返回false
*/
public static boolean getBoolean(Context context, String key) {
return getBoolean(context, key, false);
}
/**
* 获取boolean类型数据
*
* @param context
* @param key
* @param defaultValue
* @return
*/
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
SharedPreferences settings = context.getSharedPreferences(APP_Prefs_Name, Context.MODE_PRIVATE);
return settings.getBoolean(key, defaultValue);
}
}
文件被保存在内部存储中时,默认情况下,文件是应用程序私有的,其他应用不能访问。当用户卸载应用程序时这些文件也跟着被删除。
文件默认存储位置:/data/data/包名/files/文件名。
Android系统默认提供了两个方法openFileOutput()、openFileInput()将数据保存到内部存储器中。使用方法如下:String text = "my name is lly";其中openFileOutput(String fileName,int mode),第一个参数指定要打开的文件名称(不需要写路径,因为文件 默认存储位置:/data/data/包名/files/文件名),如果没有这个文件,则自动创建一个;
try {
FileOutputStream fos = openFileOutput("lly.txt", Context.MODE_PRIVATE);
fos.write(text.getBytes());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Context.MODE_PRIVATE = 0
为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容。
Context.MODE_APPEND = 32768
该模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLE = 1
表示当前文件可以被其他应用读取。
MODE_WORLD_WRITEABLE
表示当前文件可以被其他应用写入。
try {
FileInputStream fis = openFileInput("lly.txt");
byte[] buf = new byte[1024];
StringBuilder sb = new StringBuilder();
int len = 0;
while((len = fis.read(buf)) != -1 ){
sb.append(new String(buf,0,len));
}
fis.close();
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
//获取外存储的状态
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// 可读可写
mExternalStorageAvailable = mExternalStorageWriteable =true;
} elseif (
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // 可读
} else {
// 可能有很多其他的状态,但是我们只需要知道,不能读也不能写
}
File file3 = new File(getExternalCacheDir().getAbsolutePath(), "getExternalCacheDir.txt");
try {
OutputStream outputStream1 = new FileOutputStream(file3);
outputStream1.write("getExternalCacheDir".getBytes());
outputStream1.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.d("TAG", "file3=" + file3);
File file4 = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "getExternalFilesDir.txt");
try {
OutputStream outputStream1 = new FileOutputStream(file4);
outputStream1.write("getExternalFilesDir".getBytes());
outputStream1.close();
} catch (Exception e) {
e.printStackTrace();
}
- 02-03 08:11:38.860 9096-9096/? D/TAG﹕ file3=/storage/emulated/0/Android/data/com.socks.baidudemo/cache/getExternalCacheDir.txt
- 02-03 08:11:38.860 9096-9096/? D/TAG﹕ file4=/storage/emulated/0/Android/data/com.socks.baidudemo/files/Pictures/getExternalFilesDir.txt
如果我们想缓存图片等比较耗空间的文件,推荐放在getExternalCacheDir()所在的文件下面,这个文件和getCacheDir()很像,都可以放缓存文件,在APP被卸载的时候,都会被系统删除,而且缓存的内容对其他APP是相对私有的。
如果你的APP产生的文件不需要隐藏,即对用户是可见的,那么你可以把文件放在外部的公共存储文件下面。我们可以通过下面的代码获取到公共存储目录:
Environment.getExternalStorageDirectory()
public class DBOpenHelper extends SQLiteOpenHelper {
private static final String DATABASENAME = "itcast.db"; //数据库名称
private static final int DATABASEVERSION = 2;//数据库版本
public DBOpenHelper(Context context) {
super(context, DATABASENAME, null, DATABASEVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20), amount integer)");//执行有更改的sql语句
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS person");
onCreate(db);
}
}
public class PersonService {
private DBOpenHelper dbOpenHelper;
public PersonService(Context context) {
this.dbOpenHelper = new DBOpenHelper(context);
}
public void payment(){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.beginTransaction();//事启事务
try{
db.execSQL("update person set amount=amount-10 where personid=?", new Object[]{1});
db.execSQL("update person set amount=amount+10 where personid=?", new Object[]{2});
db.setTransactionSuccessful();//设置事务标志为成功,当结束事务时就会提交事务
}finally{
db.endTransaction();
}
}
public void save(Person person){
//如果要对数据进行更改,就调用此方法得到用于操作数据库的实例,该方法以读和写方式打开数据库
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("insert into person (name,amount) values(?,?)",
new Object[]{person.getName(),person.getAmount()});
}
public void update(Person person){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("update person set name=? where personid=?",
new Object[]{person.getName(),person.getId()});
}
public void delete(Integer id){
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.execSQL("delete from person where personid=?", new Object[]{id.toString()});
}
public Person find(Integer id){
//如果只对数据进行读取,建议使用此方法
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from person where personid=?", new String[]{id.toString()});
if(cursor.moveToFirst()){
int personid = cursor.getInt(cursor.getColumnIndex("personid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
Person person = new Person(personid, name);
person.setAmount(amount);
return person;
}
return null;
}
public List<Person> getScrollData(Integer offset, Integer maxResult){
List<Person> persons = new ArrayList<Person>();
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from person limit ?,?",
new String[]{offset.toString(), maxResult.toString()});
while(cursor.moveToNext()){
int personid = cursor.getInt(cursor.getColumnIndex("personid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
Person person = new Person(personid, name);
person.setAmount(amount);
persons.add(person);
}
cursor.close();
return persons;
}
public Cursor getCursorScrollData(Integer offset, Integer maxResult){
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
return db.rawQuery("select personid as _id, name, amount from person limit ?,?",
new String[]{offset.toString(), maxResult.toString()});
}
public long getCount() {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from person", null);
cursor.moveToFirst();
return cursor.getLong(0);
}
}
PersonService personService = new PersonService(this.getContext());
Person person = new Person();
person.setName("xiaoxiao");
person.setAmount(100);
personService.save(person);
public class PersonProvider extends ContentProvider {
private DBOpenHelper dbOpenHelper;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final int PERSONS = 1;
private static final int PERSON = 2;
//对外公布两种对共享数据的操作方式:集合操作方式(批量方式)、单条记录操作方式
static{
MATCHER.addURI("cn.itcast.providers.personprovider", "person", PERSONS);//以集合方式进行操作(批量操作)
MATCHER.addURI("cn.itcast.providers.personprovider", "person/#", PERSON);//单条记录操作
}
@Override
public boolean onCreate() {
this.dbOpenHelper = new DBOpenHelper(this.getContext());
return false;
}
@Override
public String getType(Uri uri) {//返回当前操作的数据的mimeType
switch (MATCHER.match(uri)) {
case PERSONS:
return "vnd.android.cursor.dir/person";//对集合的操作
case PERSON:
return "vnd.android.cursor.item/person";//对单条记录的操作
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
//删除person表中的所有记录 /person
//删除person表中指定id的记录 /person/10
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int count = 0;
switch (MATCHER.match(uri)) {
case PERSONS:
count = db.delete("person", selection, selectionArgs);
return count;
case PERSON:
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
count = db.delete("person", where, selectionArgs);
return count;
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {// /person
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
long rowid = db.insert("person", "name", values);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);//得到代表新增记录的Uri
this.getContext().getContentResolver().notifyChange(uri, null);
return insertUri;
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
//查询person表中的所有记录 /person
//查询person表中指定id的记录 /person/10
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (MATCHER.match(uri)) {
case PERSONS:
return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
case PERSON:
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
return db.query("person", projection, where, selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
//更新person表中的所有记录 /person
//更新person表中指定id的记录 /person/10
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int count = 0;
switch (MATCHER.match(uri)) {
case PERSONS:
count = db.update("person", values, selection, selectionArgs);
return count;
case PERSON:
long id = ContentUris.parseId(uri);
String where = "personid="+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
count = db.update("person", values, where, selectionArgs);
return count;
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
}