当前位置: 编程技术>移动开发
本页文章导读:
▪定时更新UI之service和broadcast 定时更新UI之service跟broadcast
使用service跟broadcast可以定时更新UI,这里简单复习了一下service跟broadcast的一些基本使用。
在使用的时候需要注意对应的更新UI的那个广播需要在程序里面.........
▪ acitivty 起动网络设置页面 acitivty 启动网络设置页面
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
......
▪ 获取资料流的长度 获取文件流的长度
FILE* mFile = fopen("xxx_file", "rb");
fseek(mFile, 0, SEEK_END);
int size = ftello(mFile);
......
[1]定时更新UI之service和broadcast
来源: 互联网 发布时间: 2014-02-18
定时更新UI之service跟broadcast
使用service跟broadcast可以定时更新UI,这里简单复习了一下service跟broadcast的一些基本使用。
在使用的时候需要注意对应的更新UI的那个广播需要在程序里面进行注册,因为这样可以确保onReceive方法里面回调的那个content是UI对应的activity,如此可以进行转换或许对应的ui控件进行更新。
下面是一个简单的定时器小应用,这里直接贴代码了:
TimeActivity :
private TimeBroadcastReceiver receiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
receiver = new TimeBroadcastReceiver();
startService(new Intent(this, TimeService.class));
registerReceiver(receiver, new IntentFilter(getString(R.string.time_broad_action)));
}
@Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
}
TimeService:
@Override
public void onCreate() {
super.onCreate();
startSyncTime();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void startSyncTime(){
final SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日hh:mm:ss");
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent(getString(R.string.time_broad_action));
intent.putExtra("time", df.format(new Date()));
sendBroadcast(intent);
}
}, 1000, 1000);
}
TimeBroadcastReceiver:
@Override
public void onReceive(Context context, Intent intent) {
TimeServiceActivity timeAct = (TimeServiceActivity) context;
TextView tv_time = (TextView) timeAct.findViewById(R.id.tv_time);
tv_time.setText(intent.getExtras().getString("time"));
}
[2] acitivty 起动网络设置页面
来源: 互联网 发布时间: 2014-02-18
acitivty 启动网络设置页面
startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
[3] 获取资料流的长度
来源: 互联网 发布时间: 2014-02-18
获取文件流的长度
FILE* mFile = fopen("xxx_file", "rb");
fseek(mFile, 0, SEEK_END);
int size = ftello(mFile);
最新技术文章: