- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写非常简单的应用程序。它工作正常,但是当我按下后退键时它崩溃了(Activity 泄露了最初在此处注册的 IntentReceiver)。这个应用程序应该在后台运行。如何正确执行?
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class Bateria extends Activity implements OnClickListener, OnSeekBarChangeListener {
ProgressBar pbBatteryLevel;
TextView tvBatteryLevel;
TextView tvLeft;
RadioButton rbPercent;
RadioButton rbTime;
SeekBar sbSetLeft;
boolean percent; // true - percent, false - time
boolean informed;
int progress;
int MAX_TIME;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
PowerManager pM = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wL = pM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "whatever");
long HOURS24 = 86400000;
super.onCreate(savedInstanceState);
wL.acquire(HOURS24);
setContentView(R.layout.main);
initialize();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
registerReceiver(batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private void initialize() {
// TODO Auto-generated method stub
SharedPreferences preferences = getSharedPreferences("SP_BATTERY", 0);
percent = preferences.getBoolean("percent", true);
progress = preferences.getInt("progress", 0);
informed = false;
MAX_TIME = 72*60;
pbBatteryLevel = (ProgressBar) findViewById(R.id.pbBatteryLevel);
tvBatteryLevel = (TextView) findViewById(R.id.tvBatteryLevel);
tvBatteryLevel.setTextSize(20);
tvLeft = (TextView) findViewById(R.id.tvLeft);
if(percent)
tvLeft.setText(String.valueOf(progress) + " %");
else{
int minutes = MAX_TIME*progress/100;
int hours = minutes / 60;
minutes -= hours*60;
tvLeft.setText(String.valueOf(hours) + "h " + String.valueOf(minutes) + "min");
}
rbPercent = (RadioButton) findViewById(R.id.rbPercent);
rbPercent.setOnClickListener(this);
rbPercent.setChecked(percent);
rbTime = (RadioButton) findViewById(R.id.rbTime);
rbTime.setOnClickListener(this);
rbTime.setChecked(!percent);
sbSetLeft = (SeekBar) findViewById(R.id.sbSetPercent);
sbSetLeft.setOnSeekBarChangeListener(this);
sbSetLeft.setProgress(progress);
registerReceiver(batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent i) {
int level = i.getIntExtra("level", 0);
int voltage = i.getIntExtra("voltage", 0);
int temperature = i.getIntExtra("temperature", 0);
pbBatteryLevel.setProgress(level);
tvBatteryLevel.setText("Battery Level: " + Integer.toString(level)
+ "%" + "\n" + voltage + " mV " + temperature + " C");
if(level < progress && !informed){
//////////
Toast.makeText(getApplicationContext(), "BATTERY", Toast.LENGTH_LONG).show();
MediaPlayer mp = MediaPlayer.create(Bateria.this, R.raw.explosion);
mp.start();
//////////
informed = true;
}
else if(level >= progress){
informed = false;
}
}
};
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case(R.id.rbPercent):
percent = true;
tvLeft.setText(String.valueOf(progress) + " %");
break;
case(R.id.rbTime):
percent = false;
int minutes = MAX_TIME*progress/100;
int hours = minutes / 60;
minutes -= hours*60;
tvLeft.setText(String.valueOf(hours) + "h " + String.valueOf(minutes) + "min");
break;
}
SharedPreferences preferences = getSharedPreferences("SP_BATTERY", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("percent", percent);
editor.commit();
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if(percent)
tvLeft.setText(String.valueOf(progress) + " %");
else{
int minutes = MAX_TIME*progress/100;
int hours = minutes / 60;
minutes -= hours*60;
tvLeft.setText(String.valueOf(hours) + "h " + String.valueOf(minutes) + "min");
}
this.progress = progress;
SharedPreferences preferences = getSharedPreferences("SP_BATTERY", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("progress", progress);
editor.commit();
informed = false;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
System.exit(0);
return false;
}
}
最佳答案
首先,您需要根据需要注销已注册的 BroadcastReceiver。有关详细信息,请参见此处:http://developer.android.com/reference/android/content/BroadcastReceiver.html
更重要的是,您不能仅通过开发 Activity 来开发在后台运行的应用程序。您需要为此开发一项服务。查看此链接以了解如何执行此操作以及 API 文档:http://developer.android.com/reference/android/app/Service.html
关于Android - Activity 已泄漏 IntentReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10710984/
我的 onCreate() 中有以下代码: registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_
我在特定 Activity 中通过单击按钮打开发送菜单: Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); i.p
我使用 addProximityAlert: Intent intent = new Intent(getString(R.string.intent_message_location_fetched
在我的 Android 应用程序中,我有一个 Activity MainActivity,它有一个 Fragment MainFragment,它启动一个 IntentService,然后转换到 Ac
E/ActivityThread( 655): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver
我正在编写非常简单的应用程序。它工作正常,但是当我按下后退键时它崩溃了(Activity 泄露了最初在此处注册的 IntentReceiver)。这个应用程序应该在后台运行。如何正确执行? impor
我正在尝试一起发送短信和邮件。发送邮件没有问题,但是当我发送短信时收到此异常: End has leaked IntentReceiver Are you missing a call to unre
程序员。我正在做一个 Proximity Alert App,想知道如何从 ProximityIntentReceiver extends BroadcastReceiver 发送 SMS,即在进入特
在我的 Widget 中,我使用: Intent intent = new Intent(context, ClassForWidget.class); PendingIntent pendingIn
场景如下,我有一个 LoginActivity,它使用 WifiManager 来获取 IP 地址,如下所示: WifiManager wifiManager = (WifiManager)getSy
在 list 中,我只有 Activity ,接收器上没有任何服务。当我完成 Activity 时,我在 logcat 中看到了这个: E/ActivityThread: Activity com.e
mapView = (MapView) findViewById(R.id.mapview); mapView.onCreate(savedInstanceState); mapVie
我正在构建一个需要 IntentReceiver 的小应用。 其实我是Android开发新手,iOS开发后正在做switch。我有以下代码: package com.TheIntent; import
我正在制作一个安卓应用。我正在制作一项服务,该服务在用户选中 checkbox 时一直在后台运行,当用户 unckeck 时它应该停止。所以当我选中该框并且它还显示“服务启动” toast 时它确实有
我希望能在这里找到一些帮助,因为我不熟悉 Android 中的 BroadcastReceivers。这段代码打开一个 WebView,将您重定向到登录页面,并在检测到 URL 更改后接收登录 tok
在将此帖子标记为已关闭或重复之前,我想说的是,我已经完成了类似帖子中提到的所有事情,但都没有奏效。 我有 2 个接收器,我从中获取数据。所以我想在 Activity 开始时注册接收器,并在 Activ
我的 Android 应用程序中有一个 MapActivity,它使用 osmdroid(Open Street Map for Android 库)显示 map 。 当我在此 MapActivity
我正在使用前台 Android Service 通过 REST API 通过 Retrofit 请求资源,作为每 60 秒的计时器。此类服务也在与应用程序不同的进程中运行,这是必要的,这样 Andro
我希望我的应用程序在用户触摸 GLSurfaceView 时在 GLSurfaceView 上显示 ZoomButtonsController。我的 Activity 构造函数如下所示: _zoomB
IntentReceiver 正在泄漏 由于 onDetachedFromWindow 在某些情况下未被调用。 @Override protected void onDetachedFromWind
我是一名优秀的程序员,十分优秀!