gpt4 book ai didi

android - I/Choreographer : Skipped 439 frames! 应用程序可能在其主线程上做了太多工作

转载 作者:行者123 更新时间:2023-11-30 00:17:09 25 4
gpt4 key购买 nike

i want to connect the bT Module to my android mobile. But when i run my code this error is come I/Choreographer: Skipped 439 frames! The application may be doing too much work on its main thread.

package com.example.hp.classcircuit;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {
//decleare valiables
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mDevice;
ListView listView;
ConnectThread mConnectThread;
ArrayList pdal;
String DEVICE_ADDRESS;
boolean found=false;
BluetoothManager BM;
Set<BluetoothDevice> pairedDevices;

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)//for chacking sutable
//API
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView= (ListView) findViewById(R.id.listview);//Listview in xml
//call BT Manager
BM= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
//call BT Adaptor
mBluetoothAdapter=BM.getAdapter();
//call paired device
pairedDevices = mBluetoothAdapter.getBondedDevices();

if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(MainActivity.this, "Device not support bluetooth", Toast.LENGTH_LONG).show();
}
//check BT is on or off
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}

pdal=new ArrayList();

if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
pdal.add(device.getName());

}

ArrayAdapter pda=new ArrayAdapter(this, android.R.layout.simple_list_item_1,pdal);

listView.setAdapter(pda);
}
listView.setOnItemClickListener(myListClickListener);


}

**

//action click listnor for listview

private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String DeviceName=pdal.get(i).toString();
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals(DeviceName)) {

DEVICE_ADDRESS=device.getAddress();
Log.i(device.getName(), DEVICE_ADDRESS);

mDevice=device;

found=true;

break;
}
}
mConnectThread = new ConnectThread(mDevice);
//call the inner class method
mConnectThread.doInBackground();
}
};

//inner class

public class ConnectThread extends AsyncTask<String, Integer, Long>  {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
//constructor of inner class
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}

//override the methode
protected Long doInBackground(String... strings) {
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
Log.i("Status","connected");
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) { }
return null;
}
return null;
}

//function for close the socket
public void close() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}

Above is my async class .i didn't findout mistake please guide me where mistake done.Advance thanks to all

最佳答案

不要打电话

mConnectThread.doInBackground();

你所做的只是破坏了 AsyncTask 的概念并在 UI 线程上完成所有工作。

Threading rules

There are a few threading rules that must be followed for this class to work properly:

  • The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
  • The task instance must be created on the UI thread.
  • execute(Params...) must be invoked on the UI thread.
  • Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
  • The task can be executed only once (an exception will be thrown if a second execution is attempted.)

AsyncTask必须用 execute() 调用.所以将上面这行改成:

mConnectThread.execute();

请注意您定义 ConnectThread 的方式期待一些 String参数 - extends AsyncTask<String, Integer, Long> ,但是您永远不会在代码中使用此类参数。这些参数可以作为参数传递给 execute(String ... params)方法。最好将签名更改为:

public class ConnectThread extends AsyncTask<Void, Integer, Long> {
protected Long doInBackground(Void... params) {
//your code here
}
}

关于android - I/Choreographer : Skipped 439 frames! 应用程序可能在其主线程上做了太多工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47050868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com