- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想从蓝牙服务器接收一个 String
,它是 pc 我有一个 java 代码,但是当我运行我的 Android 客户端 Activity
时,它会显示这个异常
Activity com.waseem.mobileclient.bluetoot_app.MainActivity has leaked IntentReceiver com.waseem.mobileclient.bluetoot_app.MainActivity$1@410480f0 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.waseem.mobileclient.bluetoot_app.MainActivity has leaked IntentReceiver com.waseem.mobileclient.bluetoot_app.MainActivity$1@410480f0 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:792)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1111)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1098)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1092)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:365)
at com.waseem.mobileclient.bluetoot_app.MainActivity.onCreate(MainActivity.java:68)
at android.app.Activity.performCreate(Activity.java:5008)
我在 onPause()
中注销 为什么我会收到此异常 请帮帮我 这让我困扰了很长时间。这是客户端代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (myBluetoothAdapter == null) {
Log.e(TAG, "No Bluetooth Adapter available. Exiting...");
this.finish();
}
IntentFilter intent_filter=new IntentFilter(MESSAGE_RECEIVED_INTENT);
broad_receiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(MESSAGE_RECEIVED_INTENT)) {
showInformation(intent.getExtras().getString("Message"), 5000);
getVibrator().vibrate(500);
}
else {
showInformation("Message unable to receive", 5000);
getVibrator().vibrate(500);
}
}
};
this.registerReceiver(broad_receiver,intent_filter);
Button start_server = (Button)findViewById(R.id.button_start);
start_server.setOnClickListener(this);
Button stop_server = (Button)findViewById(R.id.button_stop);
stop_server.setOnClickListener(this);
}
@Override
protected void onPause() {
this.unregisterReceiver(this.broad_receiver);
super.onPause();
server.cancel();
restoreBTDeviceName();
}
@Override
public void onClick(View v) {
Button btn = (Button) v;
if (btn.getId() == R.id.button_start) {
if (!myBluetoothAdapter.getName().startsWith(PREFIX))
myBluetoothAdapter.setName(PREFIX );
if (myBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
requestBTDiscoverable();
server = new AcceptThread();
server.start();
btn.setEnabled(false);
((Button) this.findViewById(R.id.button_stop)).setEnabled(true);
}
} else if (btn.getId() == R.id.button_stop) {
server.cancel();
btn.setEnabled(false);
((Button) this.findViewById(R.id.button_start)).setEnabled(true);
restoreBTDeviceName();
}
}
/**
* Launches Discoverable Bluetooth Intent.
*/
public void requestBTDiscoverable() {
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivityForResult(i, REQUEST);
int result = 0;
this.onActivityResult(REQUEST, result, i);
Log.i(TAG, "Bluetooth discoverability enabled");
}
/**
* Obtains the Vibrator service.
*
* @return Vibrator Object.
*/
private Vibrator getVibrator() {
return (Vibrator) getSystemService(VIBRATOR_SERVICE);
}
/**
* Removes the prefix from the device name if the prefix is present.
*/
private void restoreBTDeviceName() {
if (myBluetoothAdapter.getName().startsWith(PREFIX))
myBluetoothAdapter.setName(myBluetoothAdapter.getName().substring(PREFIX.length()));
}
/**
* Shows a information dialog.
*
* @param message String resource used to define the message.
* @param duration Dialog's TTL.
*/
private void showInformation(String message, long duration) {
final Dialog mDialog = new Dialog(this);
TextView txt = new TextView(this);
txt.setText(message);
mDialog.setContentView(txt);
mDialog.setTitle("Information");
mDialog.show();
(new Handler()).postDelayed(new Runnable() {
public void run() {
mDialog.dismiss();
}
},duration); // Close dialog after delay
}
/**
* ************************************* AcceptThread *************************************
*/
class AcceptThread extends Thread {
/**
* Tag that will appear in the log.
*/
private final String ACCEPT_TAG = AcceptThread.class.getName();
/**
* The bluetooth server socket.
*/
private final BluetoothServerSocket mServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
try {
tmp = myBluetoothAdapter.listenUsingRfcommWithServiceRecord(ACCEPT_TAG, UUID.fromString(defaultUUID));
} catch (IOException e) {
e.printStackTrace();
}
mServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
while (true) {
try {
Log.i(ACCEPT_TAG, "Listening for a connection...");
socket = mServerSocket.accept();
Log.i(ACCEPT_TAG, "Connected to " + socket.getRemoteDevice().getName());
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
try {
// Read the incoming string.
String buffer;
DataInputStream in = new DataInputStream(socket.getInputStream());
buffer = in.readUTF();
Intent i = new Intent(MESSAGE_RECEIVED_INTENT);
i.putExtra("Message", String.format("%sn From: %s", buffer, socket.getRemoteDevice().getName()));
getBaseContext().sendBroadcast(i);
} catch (IOException e) {
Log.e(ACCEPT_TAG, "Error obtaining InputStream from socket");
e.printStackTrace();
}
try {
mServerSocket.close();
} catch (IOException e) {
}
break;
}
}
}
/**
* Will cancel the listening socket, and cause the thread to finish
*/
public void cancel() {
try {
mServerSocket.close();
} catch (IOException e) {
}
}
}
最佳答案
LogCat 的第一行说:
Are you missing a call to unregisterReceiver()?
简单的说就是需要查看BroadcastReceiver
的API文档寻求帮助,上面写着:
Note: If registering a receiver in your
Activity.onResume()
implementation, you should unregister it inActivity.onPause()
. (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister inActivity.onSaveInstanceState()
, because this won't be called if the user moves back in the history stack.
关于java - 为什么 IntentReceiverLeaked?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23851201/
我正在使用 BroadcastReceiver 来监控电池电量。我提到了this我的代码是 public class PowerConnectionReceiver extends Broadcast
我在使用 android 6 时遇到 WifiDirect 连接问题。直接连接 WIFI 并将后退按钮推到我的应用程序后。该应用程序泄漏了 intentreceiver public class Wi
我正在尝试下载一个文件,下载后想在数据库中更新它,但我的 Intent 是服务广播接收器造成了这个问题: android.app.IntentReceiverLeaked: Service com.d
我想从蓝牙服务器接收一个 String,它是 pc 我有一个 java 代码,但是当我运行我的 Android 客户端 Activity 时,它会显示这个异常 Activity com.waseem.
注意:带有android:excludeFromRecents="true"的 Activity 标志 @Override protected void onDestroy() {
friend 的, 我在 map View 中遇到问题,最初我有 map View ,标题中有搜索选项,用于搜索我正在使用的内容这是我的搜索代码... Intent intent = getInten
我是一名优秀的程序员,十分优秀!