标签:android c style class code a
每次广播到来时 , 会重新创建 BroadcastReceiver 对象 , 并且调用 onReceive() 方法 , 执行完以后 , 该对象即被销毁 . 当 onReceive() 方法在 10 秒内没有执行完毕, Android 会认为该程序无响应 . 所以在BroadcastReceiver 里不能做一些比较耗时的操作 , 否侧会弹出 ANR(Application NoResponse) 的对话框 .
如果需要完成一项比较耗时的工作 , 应该通过发送 Intent 给 Service, 由 Service 来完成 . 这里不能使用子线程来解决 , 因为 BroadcastReceiver 的生命周期很短 , 子线程可能还没有结束BroadcastReceiver 就先结束了 .BroadcastReceiver 一旦结束 , 此时 BroadcastReceiver 的所在进程很容易在系统需要内存时被优先杀死 , 因为它属于空进程 ( 没有任何活动组件的进程 ). 如果它的宿主进程被杀死 , 那么正在工作的子线程也会被杀死 . 所以采用子线程来解决是不可靠的 .
//实现public class MyReceiver extends BroadcastReceiver {private String tag="MyReceiver";@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubLog.i(tag, intent.getAction());}}清单中配置<uses-permission android:name="android.permission.RECEIVE_SMS"/><receiver android:name=".MyReceiver"><intent-filter ><action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver>
//注册IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");registerReceiver(mBroadcastReceiver, filter);
//广播接受者private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){@Overridepublic void onReceive(Context context, Intent intent) {Log.i(tag, "代码注册"+intent.getAction());}};//取消注册建议在onPuase()中执行unregisterReceiver(mBroadcastReceiver);
//注册IntentFilter filter = new IntentFilter("cn.lt.MYRECEIVER");registerReceiver(mBroadcastReceiver, filter);
//广播接受者private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){@Overridepublic void onReceive(Context context, Intent intent) {Log.i(tag, "代码注册"+intent.getAction());}};//发送广播Intent intent = new Intent();intent.setAction("cn.lt.MYRECEIVER");sendBroadcast(intent);
abortBroadcast()
这个方法可以截获由 sendOrderedBroadcast() 发送来的广播,让其它广播接收者无法收到这个广播
clearAbortBroadcast()
这个方法是针对 abortBroadcast() 方法,用于取消截获广播。这样它的下一级广播接收者就能够收到该广播了
getAbortBroadcast()
是否终止广播, true终止
//获取SharedPreferences对象SharedPreferences preferences = getSharedPreferences("CONFIG", Context.MODE_PRIVATE);//存储数据Editor editor = preferences.edit();editor.putString("name", "luv");editor.commit(); //提交//得到数据String mString = preferences.getString("name", "");
public class MySqlHelper extends SQLiteOpenHelper {/**** @param context 上下文* @param name 数据库名* @param factory 缺省为null* @param version 数据库版本*/public MySqlHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("CREATE TABLE person (_id integer primary key autoincrement, name varchar(30))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}//表操作实现类public class PersonDao {private String tag = "PersonDao";private MySqlHelper sqlHelper;public PersonDao(Context context) {sqlHelper = new MySqlHelper(context, "person.db", null, 1);}public void add(String name) {SQLiteDatabase db = sqlHelper.getReadableDatabase();if (db.isOpen()) {db.execSQL("insert into person (name) values(?)", new Object[]{name});db.close();}}public void all() {SQLiteDatabase db = sqlHelper.getReadableDatabase();if (db.isOpen()) {Cursor cursor = db.rawQuery("select name from person ", null);while (cursor.moveToNext()) {Log.i(tag, cursor.getString(0));}db.close();}}}//创建PersonDao实现private PersonDao personDao;personDao = new PersonDao(this);personDao.add("luv");personDao.all();
URL url = new URL("http://192.168.1.104:8080/newapk.apk");HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setReadTimeout(5000);conn.setRequestMethod("GET");if (conn.getResponseCode() == 200) {//得到文件长度int total = conn.getContentLength();pdDialog.setMax(total);InputStream is = conn.getInputStream();File file = new File(filePath);FileOutputStream fos = new FileOutputStream(file);byte[] buffer = new byte[1024];int len;int process = 0;while ((len = is.read(buffer))!= -1) {fos.write(buffer,0, len);process += len;pdDialog.setProgress(process);}fos.flush();fos.close();is.close();return file;
}
Intent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);//file -> apk 文件intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");startActivity(intent);
//创建提供者public class PersonDBProvider extends ContentProvider {//定义一个uri的匹配器用于匹配uri,如果路径不满足返回-1private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);private static final int INSERT = 1;private static final int DELETE = 2;private static final int UPDATE = 3;private static final int QUERY = 4;static {//添加匹配规则matcher.addURI("cn.lt.personprovider", "insert", INSERT);matcher.addURI("cn.lt.personprovider", "delete", DELETE);matcher.addURI("cn.lt.personprovider", "update", UPDATE);matcher.addURI("cn.lt.personprovider", "query", QUERY);}private MySqlHelper sqlHelper;@Overridepublic boolean onCreate() {// TODO Auto-generated method stubsqlHelper = new MySqlHelper(getContext(), "person.db", null, 1);return false;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {if (matcher.match(uri)==QUERY) {SQLiteDatabase db = sqlHelper.getReadableDatabase();return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);} else {throw new IllegalArgumentException("查询路径不合法");}}@Overridepublic String getType(Uri uri) {return null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {return null;}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {return 0;}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {return 0;}}
<!-- 清单中声明,其中exported表示内容提供者被其他应用程序调用,在版本17以后默认为false,需手动设为true --><providerandroid:name=".PersonDBProvider"android:authorities="cn.lt.personprovider"android:exported="true" />
//创建一个新的Android程序,获取到上面程序中提供的数据ContentResolver resolver = getContentResolver();Uri uri = Uri.parse("content://cn.lt.personprovider/query");Cursor cursor = resolver.query(uri, null, null, null, null);while (cursor.moveToNext()) {Log.i(tag , cursor.getString(0));}
//创建提供者public class
标签:android c style class code a
原文地址:http://www.cnblogs.com/lv-2012/p/3766780.html