我已经定义了一个在加载过程中显示的启动画面.但根据互联网连接,加载时间仅为600毫秒,有时甚至需要5000毫秒.所以我定义了启动画面至少显示了3000毫秒,这样用户就不会被浮雕屏幕激怒.
我通过以下方式定义启动画面的开始:
private void splashScreen() {
setContentView(R.layout.splashscreen);
splash = (ImageView) findViewById(R.id.splashscreenLayer);
startSplashTime = new Date();
new LoadingThread().start();
}
Run Code Online (Sandbox Code Playgroud)
在LoadingThread中,我检查网络并从Internet加载数据:
private class LoadingThread extends Thread {
@Override
public void run() {
checkNetwork();
}
}
Run Code Online (Sandbox Code Playgroud)
加载完成后,我将消息发送到MainActivity中定义的处理程序:
public void stopSplash() {
Message msg = new Message();
msg.what = STOPSPLASH;
Date endSplashTime = new Date();
long time = endSplashTime.getTime() - startSplashTime.getTime();
System.out.println("Time Splashscreen was displayed: " + time);
if (time < SPLASH_MIN_TIME) {
long delay = SPLASH_MIN_TIME - time;
System.out.println("Delay Splashscreen for: " + …Run Code Online (Sandbox Code Playgroud) 我有这个线程从服务器下载一些图像.因此,一旦下载图像,我就会调用处理程序并进行UI更新.因此,由于线程的stop()已弃用,我无法使用它.我这里有两个问题.
这是我的代码.
handler=new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what==0)
{
//UI Updation takes place.
}
}
};
final Thread t = new Thread(new Runnable() {
public void run() {
Log.i("Inside Thread", "Downloading Images...");
myDownlaodMethod();
handler.sendEmptyMessage(0);
}
});
t.start();
Run Code Online (Sandbox Code Playgroud) 我正在尝试以编程方式将卷提升到STREAM_MUSIC流的最大值,但是当我这样做时,我正在"向死机线程上的处理程序发送消息".此外,它似乎没有100%的时间提高音量,虽然当我得到这个错误时,它会提高它的大部分时间.
代码是:
System.out.println("Maximum volume for this stream is: "+maxstreamvol+" and it used to be set to: "+currentvol);
final AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, maxstreamvol, AudioManager.FLAG_SHOW_UI);
am.setStreamSolo(AudioManager.STREAM_MUSIC, true);
System.out.println("Volume Raised!");
Run Code Online (Sandbox Code Playgroud)
在谷歌搜索后,似乎这个错误与多线程情况有关......此时的代码应该在UI线程上运行.
事实上,我甚至把它包围着:
runOnUiThread(new Runnable() {
public void run() {
// code_goes_here
}
});
Run Code Online (Sandbox Code Playgroud)
而且,这产生了同样的错误.我看到的错误是这样的:
I/System.out(24949): Maximum volume for this stream is: 15 and it used to be set to: 0
W/MessageQueue( 490): Handler (android.media.AudioManager$FocusEventHandlerDelegate$1) {42b52f28} sending message to a Handler on a dead thread
W/MessageQueue( 490): java.lang.RuntimeException: …Run Code Online (Sandbox Code Playgroud) 我handler在一个单独的线程中完成一个耗时的任务后,使用一个对象来继续UI工作.有上述Lint警告的问题和跟随是我的方法.
[Sample Handler对象类型1] - >
Handler responseHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
Toast.makeText(MainActivity.this, "Finished the long running task in seperate thread...", Toast.LENGTH_LONG).show();
}
};
Run Code Online (Sandbox Code Playgroud)
[Sample Handler对象类型2] - >
Handler responseHandler = new Handler(new Handler.Callback()
{
@Override
public boolean handleMessage(Message msg)
{
Toast.makeText(MainActivity.this, "Finished long running task in a seperate thread...", Toast.LENGTH_LONG).show();
return false; // RETURN VALUE ????
}
});
Run Code Online (Sandbox Code Playgroud)
在完成耗时任务的单独线程(除了UI)之外,它执行以下行以将控件返回到UI线程(基本上是处理程序obj).
responseHandler.sendEmptyMessage(0);
Run Code Online (Sandbox Code Playgroud)
该程序对两种类型的处理程序对象都可以正常工作,但是对于第一种类型,我得到一个Lint警告说这个Handler类应该是静态的,否则可能会发生泄漏.
因此我开始使用第二种类型的处理程序对象来避免Lint警告,但我遇到的问题是,我不确定第二种方式返回值(true/false)的含义,它也可以使用.我在谷歌搜索了这么多,但没有得到一个确切的答案解释这个返回值.
是的,我看到这个问题已经在stackoverflow中的许多地方被要求主要重写Lint警告,但我的问题主要是关于第二种方式的返回类型并且确认它是否正常我使用第二种类型解决问题的方式处理程序Obj.
问题 - >
1). …
我正在尝试制作一款能够扫描某个蓝牙设备作为后台服务的Android应用.一旦手机在蓝牙设备的特定范围内,通过读取RSSI进行测量,后台服务将启动向用户显示的活动.如果手机移出蓝牙设备的范围,(在RSSI值超过某个阈值后),应该杀死活动.
以下是后台服务的代码:
public class BeaconScanService extends Service implements BluetoothAdapter.LeScanCallback {
private Service self = this;
public static final String UNLOCK = "unlock";
public static final String STATUS = "status";
public static final String SIGNAL = "signal";
public static final String GET_SIGNAL = "get_signal";
//desired device to find
private static final String targetMAC = "E1:BE:A8:1A:8B:A0";
//class to handle saving RSSI values and detecting Bluetooth proximity
private proxDetector proxy;
//boolean to determine if phone is in BT range
static boolean inProx = …Run Code Online (Sandbox Code Playgroud) android android-service bluetooth-lowenergy android-handler android-broadcast
我使用了一些处理程序
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
try{
Messages.onAcknowledgeReceived();
}catch(Exception e){
e.printStackTrace();
}
}
});
Run Code Online (Sandbox Code Playgroud)
它每15秒执行一次,有时它开始抛出一些警告
The Looper class instance count has over a limit(100). There should be some leakage of Looper or HandlerThread.
12-16 12:16:11.357: E/Looper(4647): Looper class instance count = 221
12-16 12:16:11.357: E/Looper(4647): Current Thread Name: IntentService[AppService]
12-16 12:16:12.316: E/Looper(4647): WARNING: The Looper class instance count has over a limit(100). There should be some leakage of Looper or HandlerThread.
12-16 12:16:12.318: E/Looper(4647): Looper class …Run Code Online (Sandbox Code Playgroud) 我想在几次内重启服务.我的代码看起来像这样(里面onStartCommand(...))
Looper.prepare();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(BackgroundService.this, BackgroundService.class);
startService(intent);
}
}, 3 * 60000);
Run Code Online (Sandbox Code Playgroud)
此代码执行时,服务正在前台运行,但似乎没有调用onStartCommand(...).有没有其他方法可以在几次内重启服务?
UPD:我发现它实际上重新开始服务,但不是在给定时间内(可能需要长达30分钟而不是3分钟).所以现在的问题是如何让它重新启动
我是一名 iOS 开发人员,最近刚尝试过 Android 开发。
在 iOS 中,我在我的代码中使用完成处理程序。
我想知道在Android开发中是否有等价物?
谢谢
Pre API 19,比updatePeriodMillis最小时间30分钟更新Widget 的首选方法是在设置AlarmManager时使用指定的间隔后使用a AlarmManager和a BroadcastReceiver接收Intent.
目前,使用下面的代码,Widget会更新,但是从Android 5.1开始,使用重复间隔小于60000ms的.setRepeating()会自动将其间隔设置为至少60000ms.
在Widgets onEnabled()中设置闹钟:
AlarmManager am= (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//After after 3 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 3000, 1000 , pi);
Run Code Online (Sandbox Code Playgroud)
然后在AlarmManagerBroadcastReceiver的onReceive()中:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
//Acquire the lock
wl.acquire();
/*
* ......
* Update Widgets RemoteViews
*/
wl.release();
Run Code Online (Sandbox Code Playgroud)
在setRepeating()的文档中,它说:
注意:从API 19开始,所有重复警报都不准确.如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,每次重新安排如上所述.targetSdkVersion早于API 19的旧应用程序将继续将所有警报(包括重复警报)视为精确警报.
它现在还说:
安排重复警报.注意:对于计时操作(刻度,超时等),使用起来更容易,效率更高
Handler
那么您将如何使用处理程序更新Widgets Remoteviews?当设备进入睡眠状态以节省电池时,您会如何停止它?
有没有其他建议的方法来更新Widget?
我正在尝试使用这篇文章来创建异步UDP套接字.
所以我有这个代码:
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpThread
extends HandlerThread {
private static final String TAG = "UDP";
private final Handler uiHandler, workerHandler;
private final DatagramSocket socket = new DatagramSocket();
public UdpThread(final Handler uiHandler, final String hostname, final int port) throws SocketException {
super(TAG);
this.uiHandler = uiHandler;
start();
workerHandler = new Handler(getLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(final Message msg) {
/*
if (msg.what == port && msg.obj == hostname) …Run Code Online (Sandbox Code Playgroud)