我必须创建一个Android应用程序,以准确的时间打开/关闭屏幕,以便通过可见光向外部设备发送类似摩尔斯电码的消息。到目前为止,我的实验都失败了,因为操作系统似乎干扰了我的“postAtTime”请求并弄乱了我的时间。
有没有人建议我至少每隔50毫秒开启/关闭(黑/白)Android屏幕,准确度为正负5%?
谢谢。
答案 0 :(得分:1)
你可以试试这个:
package some.pkg.name;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Handler handler = new Handler();
// run a thread after 5 seconds to start the home screen
handler.postDelayed(new Runnable() {
@Override
public void run() {
finish();
// start the home screen
Intent intent = new Intent(SplashScreen.this, TargetActivity.class);
SplashScreen.this.startActivity(intent);
}
}, 5000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/splash_image"/>
<activity android:name=".SplashScreen"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
答案 1 :(得分:0)
我会在View上尝试动画。您可以使用带有全屏白色视图的黑色背景,并打开和关闭View的可见性。
答案 2 :(得分:0)
使用postAtTime()
会增加太多开销。您可以尝试使用动画甚至尝试旧的,基于循环/睡眠的方法。对于动画see docs here(教程here),对于循环/ sleep:通过执行10次闪烁来查看设备上的应用,并查看需要多长时间。然后你知道单次闪光需要多少时间。相应地调整sleep()
以获得所需的闪光率。或者,您可以尝试使用Handler postDelayed()
,但在这种情况下它会相当无效。
答案 3 :(得分:0)
使用CountDownTimer类及其onTick方法。
你可以设置闪烁时间的毫秒数 在CountDownTimer的onTick menthod中编写动画逻辑代码。
您只需编写一个扩展CountdownTimer的类,然后在ur活动的onCreate中调用countdowntimerOBJECT.start()。 您可以使用带有全屏白色视图的黑色背景,并打开和关闭View的可见性。