iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 29
0
package com.example.touth;

public class listdata {
    private String x;
    private String y;
    private String id;

    public listdata(String x,String y,String id){
        this.x=x;
        this.y=y;
        this.id=id;
    }
    public String getx(){return x;};
    public String gety(){return y;};
    public String getid(){return id;};
}
package com.example.touth;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.stetho.Stetho;

import java.util.ArrayList;
import java.util.List;

import static android.icu.text.MessagePattern.ArgType.SELECT;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String DEBUG_TAG = "Velocity";
    private static String DATABASE_TABLE = "students";
    private VelocityTracker mVelocityTracker = null;
    private View myview;
    static SQLiteDatabase db;
    private StdDBHelper dbHelper;
    private TextView start;
    private report report;
    List<listdata> data =new ArrayList<>();

    // 建立SQLiteOpenHelper物件

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override

    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);

        switch(action) {
            case MotionEvent.ACTION_DOWN:
                if(mVelocityTracker == null) {
                    // Retrieve a new VelocityTracker object to watch the
                    // velocity of a motion.
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                    // Reset the velocity tracker back to its initial state.
                    mVelocityTracker.clear();
                }
                // Add a user's movement to the tracker.
                mVelocityTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.addMovement(event);
                // When you want to determine the velocity, call
                // computeCurrentVelocity(). Then call getXVelocity()
                // and getYVelocity() to retrieve the velocity for each pointer ID.
                mVelocityTracker.computeCurrentVelocity(1000);
                // Log velocity of pixels per second
                // Best practice to use VelocityTrackerCompat where possible.
                Log.d("123", "X velocity: " + mVelocityTracker.getXVelocity(pointerId));
                Log.d("123", "Y velocity: " + mVelocityTracker.getYVelocity(pointerId));

                float horizontalOffset = mVelocityTracker.getXVelocity(pointerId);
                float verticalOffset = mVelocityTracker.getYVelocity(pointerId);
                String x = String.valueOf(horizontalOffset);
                String y= String.valueOf(verticalOffset);
                String insert = "INSERT INTO students(  X,Y)" +
                        "VALUES (" + x +","+ y +");";
                db.execSQL(insert);
                break;
            case MotionEvent.ACTION_CANCEL:
                // Return a VelocityTracker object back to be re-used by others.
                mVelocityTracker.recycle();
                break;
        }
        return true;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Stetho.initializeWithDefaults(this);
        start=findViewById(R.id.start);
        start.setOnClickListener(this);
//        myview=findViewById(R.id.myview);
        dbHelper = new StdDBHelper(this);
        db = dbHelper.getWritableDatabase(); // 開啟資料庫
    }
    protected void onStop() {
        super.onStop();
        db.close(); // 關閉資料庫
    }







    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.start:
                gotostart() ;
                break;

        }
    }
private void gotostart(){
    for ( int i=0;i<data.size();i++){
//        report=new report(Float.parseFloat(data.get(i).getx()),Float.parseFloat(data.get(i).gety()));
//        final Float x= Float.parseFloat(data.get(i).getx());
//        final Float y= Float.parseFloat(data.get(i).gety());
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Instrumentation inst = new Instrumentation();
//                    inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),
//                            MotionEvent.ACTION_DOWN, x,y, 0));
//                    inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),
//                            MotionEvent.ACTION_UP,x,y, 0));
//                    Log.d("點擊位置", ""+x+y);
//                }catch(Exception e) {
//                    Log.e("Exception when ", e.toString());
//                }
//                //線程睡眠3s
////                try {
////                    Thread.sleep(3000);
////                } catch (InterruptedException e) {
////                    // TODO Auto-generated catch block
////                    e.printStackTrace();
////                }
//            }
//        }).start();

    }
    Intent intent=new Intent(MainActivity.this,MainActivity2.class);
    startActivity(intent);

}



}


package com.example.touth;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Instrumentation;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;

import java.util.ArrayList;
import java.util.List;


import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


import static com.example.touth.MainActivity.db;

public class MainActivity2 extends AppCompatActivity {

    List<listdata> data =new ArrayList<>();
    MyRecyclerViewAdapter adapter;
    ArrayList<String> x = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        adapter = new MyRecyclerViewAdapter(this, x);




         final Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String value) {

                Log.v("123",""+value);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                for (int i=0;i<data.size();i++) {
                    x.add(data.get(i).getx());
                    Log.d("x", "onComplete: "+x);
                }
            }

        };

        Observable.create(new ObservableOnSubscribe<String>(){
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
                listdata ll;
                String sarchsqlite=" SELECT X,Y" +"FROM student";
                String selectSQL = "SELECT * FROM students";
                Cursor cursor = db.rawQuery(selectSQL,null);
                while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex("id"));
                String x = cursor.getString(cursor.getColumnIndex("X"));
                String y = cursor.getString(cursor.getColumnIndex("Y"));
                ll=new listdata(x,y,id);
                data.add(ll);
                }
                e.onComplete();
            }
        }).subscribe(observer);//订阅

    }

    }

package com.example.touth;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>{

    private List<String> mData;
    private LayoutInflater mInflater;
    // 剛剛context跟list就是透過這個傳進來 然後我們設mData=data
    // 所以mData=Horse,Cow,Camel,Sheep,Goat
    // 這裡的inflater是要可以載入黃色那塊 就是recyclerview_row.xml
    MyRecyclerViewAdapter(Context context, List<String> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    //創造viewHolder

    // 用inflater載入黃色這塊,並傳入viewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
        return new ViewHolder(view);
    }

    // 這裡是黃色那塊尋找textView Id的,不找就不知道在哪
    public class ViewHolder extends RecyclerView.ViewHolder  {
        TextView TextView1,TextView2,TextView3;


        ViewHolder(View itemView) {
            super(itemView);
            TextView1 = itemView.findViewById(R.id.textView);
            TextView2 = itemView.findViewById(R.id.textView2);
            TextView3 = itemView.findViewById(R.id.textView3);
        }


    }
    // 用viewHolder綁定資料
    //position是mData的第幾個 向是第零個是Horse 第二個是Camel
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String animal = mData.get(position);
        holder.TextView1.setText(animal);
        holder.TextView2.setText(animal);
        holder.TextView3.setText(animal);
    }

    // 確定mData有幾個 , 可以試試看return1的話就只會有一個資料出來喔
    @Override
    public int getItemCount() {
        return mData.size();

}
}

package com.example.touth;

import android.annotation.SuppressLint;
import android.app.Instrumentation;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;

public class report extends Thread {
    private Float x,y;
    //400,689


    @Override
    public void run() {
        while(true)
        {
            //利用ProcessBuilder執行shell命令
	        /*String[] order = {
	                "input",
	                "tap",
	                "" + x,
	                "" + y
	        };
	        Log.d("點擊位置", x+","+y);
	        try {
	            new ProcessBuilder(order).start();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }*/
            // 可以不用在 Activity 中增加任何處理,各 Activity 都可以響應
            try {
                Instrumentation inst = new Instrumentation();
                inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),
                        MotionEvent.ACTION_DOWN, x, y, 0));
                inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),
                        MotionEvent.ACTION_UP, x, y, 0));
                Log.d("點擊位置", x+","+y);
            }catch(Exception e) {
                Log.e("Exception when sendPointerSync", e.toString());
            }
            //線程睡眠3s
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    public report(Float x,Float y){
        this.x=x;
        this.y=y;
    }
}

package com.example.touth;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class StdDBHelper  extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "Class.db";
    private static final int DATABASE_VERSION = 1;
    public StdDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String creatSQL = "CREATE TABLE students (" +
                "id INTEGER primary key AUTOINCREMENT, " +
                "X VARCHAR(250)," +
                "Y VARCHAR(250))";
        db.execSQL(creatSQL);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

上一篇
第28篇:如何操控版面上的原件
下一篇
第30篇
系列文
Android的30天學習歷程30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言