Android Studio 连接真机调试

以小米4为例,先将手机通过USB连接电脑,在设备管理器中确保驱动安装正确。

对手机的设置

1.设置手机为开发者模式(设置->关于手机->连续点击MIUI版本——开启成功)

2.在更多设置中找到系统安全设置——允许安装未知来源的应用

3.在更多设置中选择开发者选项,在开发者选项中同时勾选USB调试和USB安装的开关

对Android Studio 的设置:

1.在工具栏中找到app,点开后选择“Edit configurations”

2.选择其中的Target为 USB Device

3.运行程序

 

Done

 

 

PS:

运行程序中若报错:
    No target device found.
 点击下方的 Android Monitor, Android Studio会帮你自动识别查找设备。

发布了131 篇原创文章 · 获赞 43 · 访问量 17万+

树莓派与Android的一个简单问题,本人小白,解决不了了,求大神帮助!!!

02-21

# mainactivity ``` package com.example.rasberrypi; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Handler; import android.os.Message; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import org.json.JSONException; import org.json.JSONObject; import java.util.TimerTask; public class MainActivity extends Activity { private Handler mHandler; private TextView fire, people; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler = new Handler(); mHandler.post(new TimerProcess()); fire = findViewById(R.id.fire); people = findViewById(R.id.people); } @SuppressLint("HandlerLeak") final Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { String ret = msg.obj.toString(); switch (msg.what){ case 1: StringBuilder builder = new StringBuilder(); JSONObject jsonObject = null; try { jsonObject = new JSONObject(ret); Log.i("json", ""+jsonObject); if (jsonObject.getInt("isFire") == 0) { fire.setText("没有发现火焰"); fire.setTextColor(android.graphics.Color.GREEN); } else { fire.setText("发现火焰"); fire.setTextColor(android.graphics.Color.RED); mes("火焰传感器探测到火焰!"); } if (jsonObject.getInt("isInvaded") == 0) { people.setText("安全"); people.setTextColor(android.graphics.Color.GREEN); } else { people.setText("发现入侵者"); people.setTextColor(android.graphics.Color.RED); mes("发现有人进入房间!"); } } catch (JSONException e) { e.printStackTrace(); } break; default: break; } } }; private void getDevInformation(){ new Thread(new Runnable() { @Override public void run() { String ret = Webconn.getDev(); System.out.println("get return:"); System.out.println(ret); handler.sendMessage(handler.obtainMessage(1, ret)); } }).start(); } private class TimerProcess implements Runnable{ @Override public void run() { mHandler.postDelayed(this, 5000); getDevInformation(); } } private void mes(String m) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); Notification.Builder builder = new Notification.Builder(MainActivity.this); builder.setSmallIcon(R.mipmap.ic_launcher); //设置图标 builder.setTicker("显示第二个通知"); builder.setContentTitle("树莓派安防"); //设置标题 builder.setContentText(m); //消息内容 builder.setWhen(System.currentTimeMillis()); //发送时间 builder.setDefaults(Notification.DEFAULT_ALL); //设置默认的提示音,振动方式,灯光 builder.setAutoCancel(true);//打开程序后图标消失 /*空一句*/ Intent intent = new Intent(MainActivity.this,Activity.class); PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this, 0, intent, 0); builder.setContentIntent(pendingIntent); Notification notification = builder.build(); mNotificationManager.notify(1, notification); } } ``` # webconn ``` package com.example.rasberrypi; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Webconn { public static String getDev(){ String url = "http://192.168.1.104:5000/state/"; try { URL url1 = new URL(url); HttpURLConnection urlConnection =(HttpURLConnection) url1.openConnection(); int code = urlConnection.getResponseCode(); if(code == 200){ String strResult = urlConnection.getResponseMessage(); System.out.println(strResult); return strResult; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "null"; } } ``` # activity_main ``` <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.rasberrypi.MainActivity"> <TextView android:id="@+id/fire" android:layout_width="297dp" android:layout_height="95dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.135" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.225" /> <TextView android:id="@+id/people" android:layout_width="300dp" android:layout_height="88dp" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="10dp" tools:layout_editor_absoluteY="236dp" /> </android.support.constraint.ConstraintLayout> ``` 这里是代码,但是我的运行结果是空白图 ![图片说明](https://img-ask.csdn.net/upload/201802/21/1519186237_346292.png) Android studio中说 mainactivity中间的 jsonObject = new JSONObject(ret); String ret = Webconn.getDev(); 和webconn中的 int code = urlConnection.getResponseCode(); 报错 192.168.1.104出来的内容是 ![图片说明](https://img-ask.csdn.net/upload/201802/21/1519186346_762454.png) 希望大神可以帮忙解决 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览