- 广播服务
//发送广播
Intent it=new Intent();
it.setAction("GETNAME");
it.putExtra("name", account.getText().toString());
LoginDialog.this.getContext().sendBroadcast(it);
//接收广播
private BroadcastReceiver b=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("GETNAME")){
String getName=intent.getStringExtra("name");
txtView1.setText(getName);
}
}
};
//注册广播
IntentFilter itf=new IntentFilter();
itf.addAction("GETNAME");
this.registerReceiver(b, itf);
2. 使用正则表达式去除字符串中的首尾中括号
String b = a.replaceAll("\\[([^\\]]*)\\]", "$1");
3. 比较两个“11:15“”小时:分钟“形式的时间的大小
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeTest {
private static final String formatStr = "HH:mm";
private static SimpleDateFormat sdf=new SimpleDateFormat(formatStr);
public static void main(String args[]) throws ParseException {
String tS = "12:50";
String tE = "13:10";
if(isInZone(getLong(tS),getLong(tE),getCurrentTime())){
System.out.println("ts小于当前时间,tE 大于当前时间!");
}
if(TimeCompare(getLong(tS),getLong(tE))){
System.out.println("ts 小于 te");
}
}
private static boolean isInZone(long tStart,long tEnd,long t) throws ParseException {
return tStart <= t && t <= tEnd;
}
private static long getLong(String timeStr) throws ParseException {
return sdf.parse(timeStr).getTime();
}
private static long getCurrentTime() throws ParseException {
return getLong(sdf.format(new Date()));
}
private static boolean TimeCompare(long t1,long t2) throws ParseException {
return t1 <= t2;
}
}