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 根本不会闪烁(也不会变成红色)。你可以在互联网上到处找到类似的代码。我不知道我想念什么。有什么帮助吗?谢谢!