标签:广播 smis 强制转换 远程 ext 用户交互 sage single 选择
绑定服务
Intent intent = new Intent(this, BanZhengService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
定义这个实现类
class MyServiceconn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
zjr = (PublicBusiness) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
创建实现类对象
conn = new MyServiceconn();
在服务中定义一个类实现Ibinder接口,以在onBind方法中返回
class ZhongJianRen extends Binder implements PublicBusiness{
public void QianXian(){
//访问服务中的banZheng方法
BanZheng();
}
public void daMaJiang(){
}
}
电量改变、屏幕开关,必须使用代码注册
注册广播接收者
//创建广播接收者对象
receiver = new ScreenOnOffReceiver();
//通过IntentFilter对象指定广播接收者接收什么类型的广播
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
//注册广播接收者
registerReceiver(receiver, filter);
解除注册广播接收者
unregisterReceiver(receiver);
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("警告");
builder.setMessage("若练此功,必先自宫");
设置确定和取消按钮
builder.setPositiveButton("现在自宫", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "恭喜你自宫成功,现在程序退出", 0).show();
}
});
builder.setNegativeButton("下次再说", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "若不自宫,一定不成功", 0).show();
}
});
使用构建器创建出对话框对象
AlertDialog ad = builder.create();
ad.show();
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("选择你的性别");
final String[] items = new String[]{
"男", "女", "其他"
};
//-1表示没有默认选择
//点击侦听的导包要注意别导错
builder.setSingleChoiceItems(items, -1, new OnClickListener() {
//which表示点击的是哪一个选项
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您选择了" + items[which], 0).show();
//对话框消失
dialog.dismiss();
}
});
builder.show();
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("请选择你认为最帅的人");
final String[] items = new String[]{
"赵帅哥",
"赵师哥",
"赵老师",
"侃哥"
};
//true表示对应位置的选项被选了
final boolean[] checkedItems = new boolean[]{
true,
false,
false,
false,
};
builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
//点击某个选项,如果该选项之前没被选择,那么此时isChecked的值为true
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
}
});
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer sb = new StringBuffer();
for(int i = 0;i < items.length; i++){
sb.append(checkedItems[i] ? items[i] + " " : "");
}
Toast.makeText(MainActivity.this, sb.toString(), 0).show();
}
});
builder.show();
标签:广播 smis 强制转换 远程 ext 用户交互 sage single 选择
原文地址:http://www.cnblogs.com/DarrenChan/p/6231048.html