2017年3月26日日曜日

Android ContentProvider 内容提供程序單元測試

ContentProvider 内容提供程序單元測試

public class youContentProviderTest {
    @Test
    public void delete() throws Exception {
        Context context = InstrumentationRegistry.getContext();
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = Uri.parse("content://com.kankanla.e560.m0325a.youContentProvider/categoly/delete/");
        int i = contentResolver.delete(uri, "rowid > ?", new String[]{"10"});
        System.out.println("77777777777777777777777777777777");
        System.out.println(i);
    }

    @Test
    public void insert() throws Exception {
        Context context = InstrumentationRegistry.getContext();
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = Uri.parse("content://com.kankanla.e560.m0325a.youContentProvider/category/insert/1");
        System.out.println("11111111111111111111111111111111111111111");
        System.out.println(uri.getAuthority());
        System.out.println(uri.getPath());
        System.out.println(uri.getPathSegments());
        System.out.println("2222222222222222222222222222222222222222222");
        ContentValues contentValues = new ContentValues();
        contentValues.put("category_name", "日本語");
        for (int i = 0; i < 10; i++) {
            Uri uri1 = contentResolver.insert(uri, contentValues);
            System.out.println(uri1.toString());
        }
    }

}




package com.kankanla.e560.m0325a;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by E560 on 2017/03/25.
 */

public class youContentProvider extends ContentProvider {
    private static final String TAG = "youContentProvider";
    private static YouDB youDB;
    private static UriMatcher uriMatcher;
    private static final String AUTHORITY = "com.kankanla.e560.m0325a.youContentProvider";

    private static final int insert_category = 1;
    private static final int select_category = 2;
    private static final int insert_list = 3;
    private static final int select_list = 4;
    private static final int matcher_all = 5;
    private static final int del_category = 6;
    private static final int error = 9;

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, "list/insert", insert_list);
        uriMatcher.addURI(AUTHORITY, "category/insert", insert_category);
        uriMatcher.addURI(AUTHORITY, "category/insert/#", insert_category);
        uriMatcher.addURI(AUTHORITY, "category/delete/", del_category);
        uriMatcher.addURI(AUTHORITY, "*/*/*", error);
    }

    @Override
    public boolean onCreate() {
        youDB = new YouDB(getContext());
        SQLiteDatabase sqLiteDatabase = youDB.getReadableDatabase();
        sqLiteDatabase.isReadOnly();
        System.out.println("888888888888888888888888888888888888888888");
        Log.i(TAG, String.valueOf(sqLiteDatabase.isOpen()));
        return sqLiteDatabase.isOpen();
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Uri uri1 = Uri.parse(uri.getAuthority() + uri.getPath());
        int i = uriMatcher.match(uri);
        System.out.println(i);
        long cont = 0;
        System.out.println("000000000000000000000000000000000000000000000000");
        SQLiteDatabase sqLiteDatabase = youDB.getWritableDatabase();
        switch (i) {
            case insert_category:
                cont = sqLiteDatabase.insert("category", null, values);
                uri1 = ContentUris.withAppendedId(Uri.parse(uri.getAuthority() + uri.getPath()), cont);
                break;
            case insert_list:
                cont = sqLiteDatabase.insert("list", null, values);
                uri1 = ContentUris.withAppendedId(Uri.parse(uri.getAuthority() + uri.getPath()), cont);
                break;
        }
        System.out.println(uri1.toString());
        sqLiteDatabase.close();
        return uri1;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        int flag = uriMatcher.match(uri);
        int con = 0;
        SQLiteDatabase sqLiteDatabase = youDB.getWritableDatabase();
        switch (flag) {
            case del_category:
                con = sqLiteDatabase.delete("category", selection, selectionArgs);
                break;
        }
        sqLiteDatabase.close();
        return con;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    private class YouDB extends SQLiteOpenHelper {
        private static final String name = "m0325.db";
        private static final int version = 1;

        public YouDB(Context context) {
            super(context, name, null, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql_category = "create table category(rowid INTEGER PRIMARY KEY AUTOINCREMENT, category_name TEXT NOT NULL)";
            String sql_index = "create table list (rowid INTEGER PRIMARY KEY AUTOINCREMENT, category_id INTEGER NOT NULL,comment TEXT)";
            db.execSQL(sql_category);
            db.execSQL(sql_index);
        }

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

        }
    }

}





2017年3月19日日曜日

android adb error (10061)

解決步驟

C:\Users>adb devices
List of devices attached
cc13094de        device

C:\Users>adb connect 192.168.11.100
unable to connect to 192.168.11.100:5555: cannot connect to 192.168.11.100:5555: 対象のコンピューターによって拒否された ため、接続できませんでした。 (10061)

C:\Users>adb kill-server

C:\Users>adb usb
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
restarting in USB mode

C:\Users>adb tcpip 5555
restarting in TCP mode port: 5555

C:\Users>adb connect 192.168.11.100
connected to 192.168.11.100:5555

C:\Users>adb devices
List of devices attached
192.168.11.100:5555     device
cc13094de        device

2017年3月17日金曜日

Andriod Intent Camera

Andriod Intent Camera



public class MainActivity extends AppCompatActivity {
    protected File file;
    protected Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1();
    }

    protected void t1() {
        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "00000aaaa.jpg");
        uri = Uri.fromFile(file);
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, 99);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == RESULT_OK) {
            view2();
        }
    }

    protected void view2() {
        Display display = getWindowManager().getDefaultDisplay();
        Point p = new Point();
        display.getSize(p);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);

        int samsize = Math.max(options.outHeight / p.y, options.outWidth / p.x);
        options.inJustDecodeBounds = false;
        options.inSampleSize = samsize;

        ImageView imageView = (ImageView) findViewById(R.id.vvvv);
        Bitmap bt = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        imageView.setImageBitmap(bt);
    }
}

2017年3月13日月曜日

2017年3月12日日曜日

intent Camera

intent Camera



    protected void t1() {
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        Bundle bu = data.getExtras();
        Bitmap bt = (Bitmap) bu.get("data");
        imageView.setImageBitmap(bt);
    }


https://developer.android.com/training/camera/photobasics.html

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}






2017年3月6日月曜日

Android 的第一款軟體誕生

Android 的第一款軟體誕生。利用了Flickr提供的圖片查詢API按照名稱
查詢圖片,並顯示在手機上。

2016年5月開始接觸JAVA, 本軟體是邊學邊用的結果。

下一款軟體爭取能夠公開在GooglePlay上。






下載地址
app-free-release.apk
https://www.asuswebstorage.com/navigate/s/181301E935CB4FD480EB2D54B763A9A9Y

2017年2月28日火曜日

Java CallBack 练习

Java CallBack 练习

https://paiza.io/projects/J4kb3mqHFWTylgPFh_q-sw

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws Exception {
        Text1 tt1 = new Text1();
        tt1.t1(new Text1.CallBack(){
            public void gets(String x){
                System.out.println(x);
            }
            public void gety(String y){
                System.out.println(y);
            }
        });
    }
}

class Text1{
    interface CallBack{
        public void gets(String x);
        public void gety(String y);
    }

    public void t1( CallBack cb){
        int i = 6;
        for(; i < 200; i++){
        }
        if(i%2 == 0){
            cb.gets("x" + i + "");
        }else{
            cb.gety("y" + i + "");
        }
    }
}