2

当使用单击平板电脑中的按钮时,我需要启用 LED 闪烁。在这里,我使用以下代码创建了一个示例应用程序。此代码在移动设备(HTC Sensation)中运行良好。当我对平板电脑使用相同的代码时,它不起作用。为什么会发生这种情况,Log Cat 中没有错误消息。我用的是东芝 Thrive AT100 平板电脑

LED_Blinking_Activity.java

public class LED_Blinking_Activity extends Activity {

    Button on, off;
    int LED_NOTIFICATION_ID = 0;
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_led__blinking_);
        on = (Button) findViewById(R.id.button1);
        off = (Button) findViewById(R.id.button2);
        final Vibrator vibe = (Vibrator) this
                .getSystemService(VIBRATOR_SERVICE);
        on.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notif = new Notification();
                notif.ledARGB = 0xFFff0000; // Green color 0xFF00FF00
                notif.flags = Notification.FLAG_SHOW_LIGHTS;
                notif.ledOnMS = 100;
                notif.ledOffMS = 1000;
                nm.notify(LED_NOTIFICATION_ID, notif);

                vibe.vibrate(50); // 50 is time in ms

            }
        });

        off.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                ClearLED();
            }
        });
    }

    private void ClearLED() {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.cancel(LED_NOTIFICATION_ID);
    }

    private Runnable mClearLED_Task = new Runnable() {
        public void run() {
            synchronized (LED_Blinking_Activity.this) {
                ClearLED();
            }
        }
    };
}

activity_led_闪烁的.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:text="Turn on LED" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_centerVertical="true"
        android:text="Turn o LED" />

</RelativeLayout>
4

1 回答 1

2

许多 Android 平板电脑没有通知 LED。根据此产品规格,这款平板电脑似乎也没有。如果设备没有,则无法闪烁 LED。

于 2012-09-11T13:21:16.813 回答