Android studio 应用程序在不同的设备上表现不同
我创建了一个应用程序,当我在摩托罗拉Xoom上运行它时,它会像我预期的那样执行主要活动,但当我在华为手机上运行它时,它会显示Hello World,而不会执行主要活动。(Android分别为4.1.2和8。我使用基本活动和空活动创建了项目,并获得了相同的结果)。如何阻止Hello WorldAndroid studio 应用程序在不同的设备上表现不同,android-studio,Android Studio,我创建了一个应用程序,当我在摩托罗拉Xoom上运行它时,它会像我预期的那样执行主要活动,但当我在华为手机上运行它时,它会显示Hello World,而不会执行主要活动。(Android分别为4.1.2和8。我使用基本活动和空活动创建了项目,并获得了相同的结果)。如何阻止Hello World package com.example.xoom; import android.hardware.usb.UsbConstants; import android.os.Bundle; import
package com.example.xoom;
import android.hardware.usb.UsbConstants;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.app.PendingIntent;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity
{
final Button[] pitchButton = new Button[50];
int startLength;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String ACTION_USB_PERMISSION = "com.example.xoom";
byte[] sendChar = {0x5A}; // ASCII "Z"
byte[] getChar = new byte[200];
boolean addListener;
TextView tv = new TextView(this);
int buttonCount = 0;
for (int i = 0; i < 50; i++)
{
pitchButton[i] = new Button(this);
pitchButton[i].setTypeface(Typeface.MONOSPACE);
}
tv.setTextSize(20);
getWindow().getDecorView().setBackgroundColor(Color.CYAN);
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
if (manager.getDeviceList().isEmpty()) // The device list is empty
{
tv.setTextColor(Color.RED);
tv.append("No USB Devices Found");
}
else
{
tv.setTextColor(Color.BLUE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
UsbDevice device = deviceIterator.next(); // Get first device in list
PendingIntent pi = PendingIntent.getBroadcast(this, 0,
new Intent(ACTION_USB_PERMISSION), 0);
manager.requestPermission(device, pi);
UsbDeviceConnection connection = manager.openDevice(device);
getDeviceProperties(device, manager, tv); // Show USB device properties
getInterfaceProperties(device, connection, tv); // Show USB Interface properties
tv.setText(""); // Clear information from above because you don't want to see
// it normally but sometimes you do. Make this statement
// a comment if you do.
setConnectionProperties(connection); // Set speed etc of connection
UsbEndpoint endpointOut = device.getInterface(1).getEndpoint(1);
UsbEndpoint endpointIn = device.getInterface(1).getEndpoint(0);
connection.bulkTransfer(endpointOut, sendChar, 1, 500); // Send request for data
//
// It takes more than one read to get the data out of the FIFO
// Build up message by polling the FIFO until there is nothing left
// This has taken a lot of fiddling about to make it work. The baud
// and the timeouts have to be just so.
// (9600 and 200 work here but why who knows (or cares))
//
int k = 0;
char[] labelStr = new char[20];
int flag = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
int bytesReceived = connection.bulkTransfer(endpointIn, getChar, 200, 200);
while (bytesReceived > 0)
{
for (int j = 0; j < bytesReceived; j++)
{
labelStr[k++] = (char) getChar[j];
if (getChar[j] == 13) // Carriage return
{
String label = String.valueOf(labelStr, 1, k - 1);
SpannableString spanLabel = new SpannableString(label);
spanLabel.setSpan(new RelativeSizeSpan(1.0f), 0, spanLabel.length(), flag);
addListener = true;
if (label.contains("Cross Slide"))
{ // Start adding pitches
spanLabel = new SpannableString("Pitch");
spanLabel.setSpan(new ForegroundColorSpan(Color.RED), 0, spanLabel.length(), flag);
addListener = false;
}
if (label.contains("End"))
{ // Start adding lengths
spanLabel = new SpannableString("Length");
spanLabel.setSpan(new ForegroundColorSpan(Color.RED), 0, spanLabel.length(), flag);
addListener = false;
if (startLength == 0) // Avoid the second "End" messages from MCU
startLength = buttonCount;
}
pitchButton[buttonCount].setText(spanLabel);
pitchButton[buttonCount].setId(buttonCount);
if (addListener)
{
pitchButton[buttonCount].setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int buttonIndex;
Button buttonText = (Button) v;
String bt = buttonText.getText().toString();
String message;
if (bt.contains(".")) // On Button press display selected value
{
message = "Pitch ";
buttonIndex = 0;
}
else
{
message = "Length ";
buttonIndex = startLength;
}
message = message + bt;
pitchButton[buttonIndex].setText(message);
pitchButton[buttonIndex].setBackgroundColor(Color.GREEN);
}
});
}
buttonCount++;
k = 0;
}
}
bytesReceived = connection.bulkTransfer(endpointIn, getChar, 200, 200);
}
connection.close();
RelativeLayout.LayoutParams[] params = new RelativeLayout.LayoutParams[100];
RelativeLayout rel = new RelativeLayout(this);
rel.addView(tv);
int leftMargin = 0;
int topMargin = 0;
int lineCount = 0;
for (int i = 0; i < buttonCount - 1; i++)
{
params[i] = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params[i].width = 150;
params[i].height = 50;
if (Math.IEEEremainder(lineCount, 5) == 0) // Put 5 buttons per line
{
topMargin = topMargin + params[i].height;
leftMargin = 0;
}
lineCount++;
if (pitchButton[i].getText().toString().contains("Pitch"))
{
topMargin = topMargin + params[i].height;
leftMargin = 0;
lineCount = 0;
}
if (pitchButton[i].getText().toString().contains("Length"))
{
topMargin = topMargin + params[i].height;
leftMargin = 0;
lineCount = 0;
}
params[i].leftMargin = leftMargin;
leftMargin = leftMargin + params[i].width;
params[i].topMargin = topMargin;
pitchButton[i].setLayoutParams(params[i]);
rel.addView(pitchButton[i]);
}
//
//Add control for setting taper
//
ArrayList<String> taperArray = new ArrayList<String>();
taperArray.add("1");
taperArray.add("2");
taperArray.add("3");
Spinner spinner = new Spinner(this,Spinner.MODE_DIALOG);
spinner.setBackgroundColor(Color.BLUE);
String taperPrompt = "Set Taper Angle";
RelativeLayout.LayoutParams spinnerParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
spinnerParams.width = 50;
spinnerParams.height = 40;
spinnerParams.topMargin = 600;
spinnerParams.leftMargin = 200;
spinner.setPrompt("Select Taper Angle");
spinner.setLayoutParams(spinnerParams);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, taperArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
}
public void onNothingSelected(AdapterView<?> adapterView)
{
}
});
RelativeLayout.LayoutParams taperParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
taperParams.width = 200;
taperParams.height = 50;
taperParams.topMargin = 600;
taperParams.leftMargin = 10;
TextView taperView = new TextView(this);
taperView.setLayoutParams(taperParams);
taperView.setText(taperPrompt);
taperView.setTextColor(Color.RED);
taperView.setTextSize(20);
rel.addView(taperView);
rel.addView(spinner);
setContentView(rel);
}
}
public void setConnectionProperties(UsbDeviceConnection connection)
{
connection.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
// mConnection.controlTransfer(0×40,
// 0, 1, 0, null, 0,
// 0);//clear Rx
connection.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
connection.controlTransfer(0x40, 0x02, 0x0000, 0, null, 0, 0);// flow
// control
// none
connection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);// baudrate
// 9600 (4138 is 9600, 1A is 115200 0271 is 4800)
connection.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0);// data bit
// 8, parity
// none,
// stop bit
// 1, tx off
}
public void getInterfaceProperties(UsbDevice device, UsbDeviceConnection connection, TextView tv)
{
int interfaceCount = device.getInterfaceCount();
int endPointCount;
for (int i=0; i < interfaceCount-1; i++)
{
if (connection.claimInterface(device.getInterface(i), true)) {
tv.append("\nConnection Interface " + i + " Claim succeeded \n");
UsbInterface interfce = device.getInterface(i);
endPointCount = interfce.getEndpointCount();
tv.append("Endpoint count for Interface " + i + " is " + endPointCount + "\n");
for (int j = 0; j < endPointCount; j++) {
if (interfce.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_INT)
tv.append("Endpoint " + j + " Type is Xfer Int \n");
if (interfce.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
tv.append("Endpoint " + j + " Type is Bulk Xfer \n");
if (interfce.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN)
tv.append("Endpoint " + j + " Direction is IN \n");
if (interfce.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT)
tv.append("Endpoint " + j + " Direction is OUT \n");
tv.append("Max packet size = " + interfce.getEndpoint(j).getMaxPacketSize() + "\n");
}
tv.append("Interface Protocol is " + interfce.getInterfaceProtocol() + "\n");
}
}
}
public void getDeviceProperties(UsbDevice device, UsbManager manager, TextView tv)
{
tv.setTextSize(20);
tv.setTextColor(Color.BLUE);
tv.append("USB Device name = " + device.getDeviceName() + "\n");
tv.append("USB Product Id = " + device.getProductId() + "\n");
tv.append("USB Vendor Id = " + device.getVendorId() + "\n");
tv.append("USB Device Id = " + device.getDeviceId() + "\n");
if (manager.hasPermission(device)) // Can the App access the device ?
{
tv.append("Device Permission = Yes \n");
}
else
{
tv.append("Device Permission = No \n");
}
}
}
package com.example.xoom;
导入android.hardware.usb.UsbConstants;
导入android.os.Bundle;
导入androidx.appcompat.app.appcompat活动;
导入android.app.pendingent;
导入android.content.Intent;
导入android.text.SpannableString;
导入android.text.span;
导入android.text.style.ForegroundColorSpan;
导入android.text.style.RelativeSizeSpan;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.RelativeLayout;
导入android.widget.Spinner;
导入android.widget.TextView;
导入android.widget.Button;
导入android.content.Context;
导入android.hardware.usb.UsbManager;
导入android.hardware.usb.UsbDevice;
导入android.hardware.usb.UsbDeviceConnection;
导入android.hardware.usb.UsbEndpoint;
导入android.hardware.usb.usb接口;
导入android.graphics.Color;
导入android.graphics.Typeface;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.HashMap;
公共类MainActivity扩展了AppCompatActivity
{
最终按钮[]pitchButton=新按钮[50];
长度;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
字符串操作\u USB\u PERMISSION=“com.example.xoom”;
字节[]sendChar={0x5A};//ASCII“Z”
字节[]getChar=新字节[200];
布尔addListener;
TextView tv=新的TextView(此);
int buttonCount=0;
对于(int i=0;i<50;i++)
{
pitchButton[i]=新按钮(此按钮);
pitchButton[i].setTypeface(Typeface.MONOSPACE);
}
电视节目(20);
getWindow().getDecorView().setBackgroundColor(Color.CYAN);
UsbManager manager=(UsbManager)getSystemService(Context.USB_服务);
if(manager.getDeviceList().isEmpty())//设备列表为空
{
tv.setTextColor(颜色:红色);
附加(“未找到USB设备”);
}
其他的
{
tv.setTextColor(Color.BLUE);
HashMap deviceList=manager.getDeviceList();
迭代器deviceIterator=deviceList.values().Iterator();
UsbDevice device=deviceIterator.next();//获取列表中的第一个设备
PendingIntent pi=PendingIntent.getBroadcast(这个,0,
新意图(操作权限),0);
manager.requestPermission(设备,pi);
UsbDeviceConnection=manager.openDevice(设备);
getDeviceProperties(设备、管理器、电视);//显示USB设备属性
getInterfaceProperties(设备、连接、电视);//显示USB接口属性
tv.setText(“”;//清除上面的信息,因为您不想看到
//这是正常的,但有时你会这样做
//如果你这样做了,请发表评论。
setConnectionProperties(连接);//设置连接的速度等
UsbEndpoint endpointOut=device.getInterface(1.getEndpoint(1);
UsbEndpoint endpointIn=device.getInterface(1.getEndpoint(0);
connection.bulkTransfer(endpointOut,sendChar,1500);//发送数据请求
//
//从FIFO中读取数据需要多次读取
//通过轮询先进先出(FIFO)建立消息,直到什么都没有留下
//这花了很多时间才使它工作。波特率
//暂停时间也必须如此。
//(9600和200在这里工作,但为什么谁知道(或关心))
//
int k=0;
char[]labelStr=新字符[20];
int flag=SPAN.SPAN_EXCLUSIVE_EXCLUSIVE;
int bytesReceived=connection.bulkTransfer(endpointIn,getChar,200200);
而(接收字节数>0)
{
对于(int j=0;j