1.Stecho简介

facebook开源的android调试工具,可以在网页上看到当前adb连接的android设备的布局层级信息,网络信息,数据库信息。

2.使用方式

添加依赖

dependencies { 
    compile 'com.facebook.stetho:stetho:1.5.0' 
    compile 'com.facebook.stetho:stetho-okhttp3:1.5.0'
} 

这是使用okhttp3的方式访问网络,如果需要用其它方式可见官网配置
http://compile ‘com.facebook.stetho:stetho-okhttp3:1.5.0’

初始化:

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Stetho.initializeWithDefaults(this);
    }
}

然后用浏览器访问 chrome://inspect/#devices
在这里插入图片描述
再点击要查看的设备的inspect,会出现下面页面:
布局:
在这里插入图片描述
网络信息:
在这里插入图片描述
数据库:
在这里插入图片描述

3.测试代码

MyApplication.java

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Stetho.initializeWithDefaults(this);
    }
}

MainActivity.java

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.lcp.stethotest.db.MySqliteOpenHelper;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView viewById;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewById = (TextView) findViewById(R.id.text);
    }

    public void post(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient.Builder()
                        .addNetworkInterceptor(new StethoInterceptor())
                        .build();
                String url = "https://www.baidu.com/";
                Request build = new Request.Builder().url(url).build();
                Call call = okHttpClient.newCall(build);
                try {
                    Response execute = call.execute();
                    Log.i("TAG", execute.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    int i = 18;
    public void add(View view){
        db = MySqliteOpenHelper.getInstance(this).getWritableDatabase();
        ++i;
        db.execSQL("insert into person(name,age) values(?,?)", new Object[]{"Mary"+(i),i});
        db.close();
    }
}

MySqliteOpenHelper.java

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqliteOpenHelper extends SQLiteOpenHelper {
    private static MySqliteOpenHelper instance;
    private static String name = "person.db";

    private MySqliteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public static MySqliteOpenHelper getInstance(Context context) {
        if (instance == null) {
            instance = new MySqliteOpenHelper(context, name, null, 1);
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table person(id integer primary key autoincrement,name text,age integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("alter table person add age1 integer null");
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.lcp.stethotest.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="post"
        android:text="post"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="add"
        android:text="add"
        />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</LinearLayout>

权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐