c++ vector(向量)應用辦法詳解(次序拜訪vector的多種方法)。本站提示廣大學習愛好者:(c++ vector(向量)應用辦法詳解(次序拜訪vector的多種方法))文章只能為提供參考,不一定能成為您想要的結果。以下是c++ vector(向量)應用辦法詳解(次序拜訪vector的多種方法)正文
這個是看知乎的時刻發明的一個成績,感到挺成心思,就將本身碰到的坑記載上去。
1、Andorid L theme colorPrimary 不克不及應用帶有alpha的色彩值,不然會有異常拋出, 直接斷定了能否alpha能否等於0或許255,其他都邑異常
@Override
protected void onApplyThemeResource(Resources.Theme theme, int resid,
boolean first) {
if (mParent == null) {
super.onApplyThemeResource(theme, resid, first);
} else {
try {
theme.setTo(mParent.getTheme());
} catch (Exception e) {
// Empty
}
theme.applyStyle(resid, false);
}
// Get the primary color and update the TaskDescription for this activity
if (theme != null) {
TypedArray a = theme.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
int colorPrimary = a.getColor(com.android.internal.R.styleable.Theme_colorPrimary, 0);
a.recycle();
if (colorPrimary != 0) {
ActivityManager.TaskDescription v = new ActivityManager.TaskDescription(null, null,
colorPrimary);
setTaskDescription(v);
}
}
}
/**
* Creates the TaskDescription to the specified values.
*
* @param label A label and description of the current state of this task.
* @param icon An icon that represents the current state of this task.
* @param colorPrimary A color to override the theme's primary color. This color must be opaque.
*/
public TaskDescription(String label, Bitmap icon, int colorPrimary) {
if ((colorPrimary != 0) && (Color.alpha(colorPrimary) != 255)) {
throw new RuntimeException("A TaskDescription's primary color should be opaque");
}
mLabel = label;
mIcon = icon;
mColorPrimary = colorPrimary;
}
2、android 5.0花屏,因為過度繪制招致,封閉硬件加快, 特別是應用webview後,能夠會有年夜幾率湧現。
3、華為手機被KILL一系列成績
用戶可以設置某個運用能否後台掩護,依照華為的功效解釋,懂得為,假如不掩護,那鎖屏後法式將沒法堅持運轉,也就是過程能夠被KILL
新裝置運用後,華為會給出選項,能否堅持,這個默許選項上存在成績,有的運用默許不許可,有的運用默許就許可。
關於耗電高被KILL成績。
關於鎖屏後收集被割斷成績。鎖屏就算掩護,而收集或許SOCKET也能夠被自動割斷。
華為本身給出了BASTET體系處理計劃,詳細不睜開。
4、雷同色彩值在全局是統一份,假如對其轉變獲得後的colorDrawable值,會招致其它一切應用的處所都轉變,可以采取mutable防止。 這個其實不克不及算作坑,是本身代碼沒有看細心。
5、華為p8手機,假如service與ui不在統一過程,service中監控收集的BroadcastReciver 會收不到收集銜接的播送,然則能收到斷開的播送,這個應當也是華為本身的優化,然則ui中的銜接與斷開都能收到播送。
6: Android 在4.4後更新了webview內核,在5.0前在webview中,不消的域可以讀取其它域設置的cookie,然則在5.0開端,體系默許值改成了false。如許會招致之前之前采取舊辦法的不克不及獲得到。(其其實我看來,確切不該該跨域來讀取cookie,多不平安)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
}
以上就是本文的全體內容,願望對年夜家的進修有所贊助。
helper.getReadableDatabase().execSQL(insert_sql); return null; } @Override public boolean onCreate() { helper=new MySQLiteOpenHelper(this.getContext(),"test.db3",null,1); return true; } @Override public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4) { String query_sql="select * from tb_test"; Cursor cursor=helper.getReadableDatabase().rawQuery(query_sql, null); return cursor; } @Override public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) { // TODO Auto-generated method stub return 0; } }listview的顯示界面show.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp" />
</LinearLayout>
Main.java
package com.app.main;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Main extends Activity {
ContentResolver resolver = null;
ListView lv = null;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) this.findViewById(R.id.listview);
resolver = this.getContentResolver();
String str = "content://com.app.test.db/";
Uri uri = Uri.parse(str);
resolver.insert(uri, null);
Cursor cursor = resolver.query(uri, null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.show, cursor,
new String[] { "name", "gender", "age" }, new int[] {
R.id.name, R.id.gender, R.id.age },
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lv.setAdapter(adapter);
}
}
完成後果:(履行了3次拔出後的後果)
ContentProvider的單位測試
ContentProvider是android的四年夜組件之一,在編寫代碼的時刻最好是加上單位測試,如許可以肯定對數據的CRUD的准確。本篇文章重要引見ContentProvider中兩個重要幫助類的應用還有單位測試的在ContentProvider中的應用。
須要用到的兩個幫助類:UriMatcher類和ContentUris類。
UriMatcher類:可以或許對輸出的uri參數就行婚配,以肯定對甚麼表履行甚麼樣的操作。
ContentUris類:有些辦法須要前往uri,應用此類可以便利的生成uri類。
關於單位測試,小我認為異常有需要在往後寫代碼的時刻應用,如許可以異常精確切實其實定代碼的准確性。
應用單位測試的步調:
1)參加instrumentation,這個部門的代碼是固定,也能夠完整在ADT供給的領導中導入。
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.android_contentprovider" >
</instrumentation>
2)添加<uses-library>,這個部門的代碼也是固定的寫法。
<uses-library android:name="android.test.runner" />
好了,必備的常識曾經講完了,如今上代碼:
1)生成一個SQLiteDatabase類,這個是必須的類MySQLiteOpenHelper類
package com.app.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static String DB_NAME = "test.db3";
private static int VERSION = 1;
public MySQLiteOpenHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//建表語句
String create_student = "create table student(_id integer primary key autoincrement,name varchar(10),age integer,gender vachar(10))";
db.execSQL(create_student);
//萬萬不克不及履行這句 // db.close();
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
然後添加我們須要的MyContentProvider類:
package com.app.contentprovider;
import com.app.db.MySQLiteOpenHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
public class MyContentProvider extends ContentProvider {
MySQLiteOpenHelper helper = null;
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
// 婚配單筆記錄
private static final int student = 1;
// 婚配多筆記錄
private static final int students = 2;
static {
matcher.addURI("com.app.wx", "student/#", student);
matcher.addURI("com.app.wx", "student", students);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = helper.getWritableDatabase();
int action = matcher.match(uri);
switch (action) {
// 婚配單筆記錄
case student:
long id = ContentUris.parseId(uri);
//獲得單筆記錄的id號
String delete_id = "_id=" + id;
if (selection != null) {
delete_id += delete_id + " and " + selection;
}
db.delete("student", delete_id, selectionArgs);
break;
// 婚配多筆記錄
case students:
db.delete("student", selection, selectionArgs);
break;
}
return 0;
}
//必須完成這個辦法,這個辦法與intent有關系,今後再講
@Override
public String getType(Uri uri) {
int code = matcher.match(uri);
switch (code) {
case student:
return "vnd.android.cursor.item/student_item";
case students:
return "vnd.android.cursor.dir/students";
default:
return null;
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = helper.getWritableDatabase();
int action = matcher.match(uri);
switch (action) {
case students:
long id1 = db.insert("student", "_id", values);
Log.i("--------", ContentUris.withAppendedId(uri, id1).toString());
return ContentUris.withAppendedId(uri, id1);
}
return null;
}
@Override
public boolean onCreate() {
helper = new MySQLiteOpenHelper(this.getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String orderBy) {
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = null;
int action = matcher.match(uri);
switch (action) {
case students:
cursor = db.query("student", projection, selection, selectionArgs,
null, null, orderBy);
break;
}
System.out.println("-----------count:" + cursor.getCount());
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] arg3) {
int count = -1;
SQLiteDatabase db = helper.getWritableDatabase();
int action = matcher.match(uri);
switch (action) {
case student:
// 以id來處置更新
long id = ContentUris.parseId(uri);
String id_selection = "_id=" + id;
if (selection != null && !selection.equals("")) {
id_selection = id_selection + " and " + values;
}
count = db.update("student", values, id_selection, arg3);
System.out.println("----------count:" + count);
break;
}
return count;
}
}
這個類很長,然則履行的辦法都是比擬罕見的CURD的辦法,主要的是UriMatcher和ContentUris類的應用。
接著履行單位測試類:Test
package com.app.contentprovider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;
public class Test extends AndroidTestCase {
public void insert() {
ContentResolver resolver = this.getContext().getContentResolver();
String str = "content://com.app.wx/student";
ContentValues values = new ContentValues();
values.put("name", "wzq");
values.put("age", 18);
values.put("gender", "boy");
resolver.insert(Uri.parse(str), values);
}
public void update() {
ContentResolver resolver = this.getContext().getContentResolver();
String str = "content://com.app.wx/student/2";
ContentValues values = new ContentValues();
values.put("name", "哈哈");
resolver.update(Uri.parse(str), values, null, null);
}
public void query() {
ContentResolver resolver = this.getContext().getContentResolver();
String str = "content://com.app.wx/student";
Uri uri = Uri.parse(str);
Cursor cursor = resolver.query(uri, new String[] { "_id",
"name,age,gender" }, null, null, "_id desc");
Log.d("------count",cursor.getCount()+"");
}
public void delete() {
ContentResolver resolver = this.getContext().getContentResolver();
String str = "content://com.app.wx/student/2";
Uri uri = Uri.parse(str);
long id=resolver.delete(uri, null, null);
}
}
履行insert辦法以後(履行了三次):
履行了update辦法以後:
履行了query辦法以後:
履行了delete辦法以後: