2
package com.example.phoneled;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class LedOnOff extends Activity {
    ToggleButton tb;
    final int ID_LED = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_led_on_off);

        tb = (ToggleButton) findViewById(R.id.toggleButton1);
        tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new Notification();
                notification.ledARGB = 0xFF0000; // 0xFF0000 red,0x00FF00 green
                notification.ledOnMS = 100;
                notification.ledOffMS = 200;
                notification.flags = Notification.FLAG_SHOW_LIGHTS;
                nm.notify(ID_LED, notification);
//              nm.cancel(ID_LED);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_led_on_off, menu);
        return true;
    }
}

以上代码用于驱动我的 Android 手机的 LED:HTC Sensation XE G18。但它不起作用。没有给出错误或警告,但真正的 LED 根本不会闪烁(也不会变成红色)。你可以在互联网上到处找到类似的代码。我不知道我想念什么。有什么帮助吗?谢谢!

4

2 回答 2

2

你有几个问题我马上就注意到了:

  1. 您必须设置默认情况下未设置的 Notification 类的某些成员,例如Notification#icon

    icon : 可绘制的资源 id,用作状态栏中的图标。这是必需的;将不会显示具有无效图标资源的通知。

  2. 与许多其他设备一样,我的 HTC 不接受自定义 LED 图案或颜色。它只会使用操作系统的默认值...

于 2012-10-10T16:37:04.277 回答
0

尝试将颜色更改为0xFFFF0000. 它应该是 ARGB,并且根据文档:

要关闭 LED,请在 alpha 通道中为 colorARGB 传递 0,或者为 ledOnMS 和 ledOffMS 传递 0。

于 2012-10-10T16:06:38.293 回答