1,通过NSNotificationCenter注册监听事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuWillShow:) name:UIMenuControllerWillShowMenuNotification object:nil];
2,当UIMenu 显示的时候调用以下方法
- (void) menuWillShow:(NSNotification *)notification { self.selectionStyle = UITableViewCellSelectionStyleBlue; if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(emailableCell:selectCellAtIndexPath:)]) { [self.delegate emailableCell:self selectCellAtIndexPath:self.indexPath]; } [[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerWillShowMenuNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuWillHide:) name:UIMenuControllerWillHideMenuNotification object:nil]; }
3,当UIMenu 消失的时候调用以下方法
- (void) menuWillHide:(NSNotification *)notification { if ((self.delegate != nil) && [self.delegate respondsToSelector:@selector(emailableCell:deselectCellAtIndexPath:)]) { [self.delegate emailableCell:self deselectCellAtIndexPath:self.indexPath]; } self.selectionStyle = UITableViewCellSelectionStyleNone; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIMenuControllerWillHideMenuNotification object:nil]; }
今天在编写Android程序的时候,遇到这个问题。通过百度和google的搜索结果,总结出以下方法:
方法一:
public class mService extends Service {
//保存在service中的Activity对象
private static mActivity m;
//启动服务
static void startservice(Context c){
m=(mActivity)c;
Intent iService=new Intent(c,mService.class);
iService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(iService);
}
//关闭服务
static void stopservice(Context c){
Intent iService=new Intent(c,mService.class);
iService.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.stopService(iService);
}
……
//在mService中关闭mActivity
m.finish();
}
public class mActivity extends Activity {
// private MediaPlayer mMPlayer;
/*
* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
……
//启动mService
mService.startservice(mActivity.this);
……
//停止mService
mService.stopservice(mActivity.this);
}
}
方法二:
主要包括3部分
1. 定义application类,这个类可以保存获取activity实例,记得manifest中加入android:name=".MyApp"
public class MyApp extends Application{
private MyServiceActivity myActivity;
public void setInstance(MyServiceActivity instance){
myActivity = instance;
}
public MyServiceActivity getInstance(){
return myActivity;
}
}
2. 在activity中保存实例
public class MyServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MyApp)getApplication()).setInstance(this);
……
}
}
3. 在service中取回实例
public class MyService extends Service {
MyServiceActivity myActivity;
@Override
public void onCreate() {
super.onCreate();
setForeground(true);
android.os.Debug.waitForDebugger();
myActivity = ((MyApp)getApplication()).getInstance();
……
}
}
package com.test.chaxun;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
public class YoubianTest {
public static final String pa_shi = "<h3><a href=/index.html"(.*)\" title=\"(.*)\">(.*)</a></h3>";
public static final String pa_xian = "<li><a href=/index.html"(.*)\" title=\"(.*)\">(.*)</a></li>";
public static final String pa_jiedao = "<td linelink\"><a href=/index.html"(.*)\" target=\"_blank\" title=\"(.*)\">(.*)</a></td>";
public static final String pa_code = "<td align=\"center\" linelink\"><a href=/index.html"(.*)\" title=\"(.*)\">(.*)</a></td>";
public static final String pa_qucode = "<td align=\"center\">(.*)</td>";
public static final String pa_zxs = "<li(.*)><a href=/index.html"(.*)\" title=\"(.*)\">(.*)</a></li>";
public static final String pa_num = "则信息, 共分为 <span (.*)\">(.*)</span> 页";
public static final String ROOT = "c:/test/";
public static final String pa_guow_1 = "<tr height=\"22\">";
public static final String pa_guow_2 = "<td linelink\"><a href=/index.html"(.*)\" target=\"_blank\">(.*)</a></td>";
public static final String pa_guow_3 = "<td>(.*)</td>";
public static final String pa_guow_4 = "td linelink\"><a href=/index.html"(.*)\" target=\"_blank\">(.*)</a></td>";
public static final String pa_guow_5 = "<td linelink\"><a href=/index.html"(.*)\" target=\"_blank\">(.*)</a></td>";
public static final String RES[] = { "http://www.youbian.com/s440000/",
"广东省", "http://www.youbian.com/s320000/", "江苏省",
"http://www.youbian.com/s330000/", "浙江省",
"http://www.youbian.com/s370000/", "山东省",
"http://www.youbian.com/s410000/", "河南省",
"http://www.youbian.com/s510000/", "四川省",
"http://www.youbian.com/s430000/", "湖南省",
"http://www.youbian.com/s360000/", "江西省",
"http://www.youbian.com/s420000/", "湖北省",
"http://www.youbian.com/s130000/", "河北省",
"http://www.youbian.com/s210000/", "辽宁省",
"http://www.youbian.com/s230000/", "黑龙江省",
"http://www.youbian.com/s350000/", "福建省",
"http://www.youbian.com/s610000/", "陕西省",
"http://www.youbian.com/s340000/", "安徽省",
"http://www.youbian.com/s220000/", "吉林省",
"http://www.youbian.com/s140000/", "山西省",
"http://www.youbian.com/s530000/", "云南省",
"http://www.youbian.com/s520000/", "贵州省",
"http://www.youbian.com/s620000/", "甘肃省",
"http://www.youbian.com/s460000/", "海南省",
"http://www.youbian.com/s630000/", "青海省",
"http://www.youbian.com/s150000/", "内蒙古自治区",
"http://www.youbian.com/s450000/", "广西自治区",
"http://www.youbian.com/s640000/", "宁夏自治区",
"http://www.youbian.com/s540000/", "西藏自治区",
"http://www.youbian.com/s650000/", "新疆自治区" };
public static void main(String[] abs) throws Exception {
build_GW();
build_ZH();
}
public static void build_GW() throws Exception {
String url = "http://www.youbian.com/guojiyoubian/list";
File f = new File(ROOT + "guowai.txt");
FileOutputStream fos1 = null;
FileOutputStream fos = null;
if (f.exists()) {
return;
} else {
f = new File(ROOT + "guowai.abs");
}
f.createNewFile();
fos1 = new FileOutputStream(f);
f = new File("c:/a.txt");
if (f.exists()) {
f.delete();
}
f.createNewFile();
fos = new FileOutputStream(f);
Pattern p1 = Pattern.compile(pa_guow_1);
Pattern p2 = Pattern.compile(pa_guow_2);
Pattern p3 = Pattern.compile(pa_guow_3);
Pattern p4 = Pattern.compile(pa_guow_4);
Pattern p5 = Pattern.compile(pa_guow_4);
int flag = 0;
String t_name = "";
String t_name2 = "";
String t_code1 = "";
String t_code2 = "";
for (int i = 1; i < 9; i++) {
String data = getNet(url + i, "utf-8");
fos.write(data.getBytes());
BufferedReader in;
in = new BufferedReader(new FileReader("c:/a.txt"));
String s = null;
while ((s = in.readLine()) != null) {
if (flag == 0) {
Matcher matcher = p1.matcher(s);
if (matcher.find()) {
flag = 1;
continue;
}
} else if (flag == 1) {
Matcher matcher = p2.matcher(s);
if (matcher.find()) {
flag = 2;
t_name = matcher.group(2);
continue;
}
} else if (flag == 2) {
Matcher matcher = p3.matcher(s);
if (matcher.find()) {
flag = 3;
t_name2 = matcher.group(1);
continue;
}
} else if (flag == 3) {
Matcher matcher = p4.matcher(s);
if (matcher.find()) {
flag = 4;
t_code1 = matcher.group(2);
continue;
}
} else if (flag == 4) {
Matcher matcher = p5.matcher(s);
if (matcher.find()) {
flag = 0;
t_code2 = matcher.group(2);
String a = (t_name.trim() + "-" + t_name2.trim() + "-"
+ t_code1 + "-" + t_code2)
+ "\n";
System.out.println(a);
fos1.write(a.getBytes());
continue;
}
}
}
}
fos1.close();
f = new File(ROOT + "guowai.abs");
File f_new = new File(ROOT + "guowai.txt");
f.renameTo(f_new);
}
/***
* 国内邮编
*
* @throws Exception
*/
public static void build_ZH() throws Exception {
Ttt t = null;
for (int u = 0; u < RES.length; u += 2) {
t = new Ttt(RES[u], RES[u + 1]);
t.start();
}
parsezxs();
}
public static class Ttt extends Thread {
public String a, b;
public Ttt(String a, String b) {
this.a = a;
this.b = b;
}
public void run() {
try {
parsep(a, b);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static final String RES2[] = { "http://www.youbian.com/c110000/",
"北京市", "http://www.youbian.com/c310000/", "上海市",
"http://www.youbian.com/c120000/", "天津市",
"http://www.youbian.com/c500000/", "重庆市" };
/**
* 解析直辖市
*/
public static void parsezxs() throws Exception {
File f = null;
FileOutputStream fos = null;
for (int i = 0; i < RES2.length; i += 2) {
String data = getNet(RES2[i], "utf-8");
String a = RES2[i + 1];
f = new File(ROOT + a);
if (!f.exists()) {
f.mkdirs();
}
f = new File(ROOT + a + "/" + a + ".txt");
if (f.exists()) {
f.delete();
}
f.createNewFile();
f = new File("c:/a.txt");
if (f.exists()) {
f.delete();
}
f.createNewFile();
fos = new FileOutputStream(f);
fos.write(data.getBytes());
Pattern pattern = Pattern.compile(pa_zxs);
BufferedReader in;
in = new BufferedReader(new FileReader("c:/a.txt"));
String s = null;
f = new File(ROOT + a + "/" + a + ".txt");
fos = new FileOutputStream(f);
while ((s = in.readLine()) != null) {
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
String ab = matcher.group(2);
String ac = matcher.group(4);
fos.write((ab.trim() + "-" + ac.trim() + "\n").getBytes());
}
}
parsefile(a);
}
}
public static void parsep(String url, String p) throws Exception {
parsequxian(url, p);
parsefile(p);
}
public static void parsefile(String p) throws Exception {
File f = new File(ROOT + p);
if (!f.exists()) {
return;
}
if (f.isFile()) {
return;
}
File fs[] = f.listFiles();
File ft = null;
BufferedReader in = null;
String s = null;
FileOutputStream fos = null;
for (int i = 0; i < fs.length; i++) {
ft = fs[i];
if (!ft.isFile()) {
continue;
}
in = new BufferedReader(new FileReader(ft));
s = in.readLine();
if (ft.length() < 20) {
s = deal(s);
if (s.equals("")) {
continue;
}
String url = "http://www.youbian.com" + s;
String aa = ft.getName();
aa = aa.substring(0, aa.length() - 4);
f = new File(ROOT + p + "/" + aa);
if (!f.exists()) {
f.mkdirs();
}
f = new File(ROOT + p + "/" + aa, aa + ".txt");
if (f.exists()) {
continue;
} else {
f = new File(ROOT + p + "/" + aa, aa + ".abs");
if (f.exists()) {
f.delete();
}
}
f.createNewFile();
fos = new FileOutputStream(f);
parsejiedao(url, fos);
// 设置重命名规则
File f_new = new File(ROOT + p + "/" + aa, aa + ".txt");
f.renameTo(f_new);
continue;
}
while ((s = in.readLine()) != null) {
String[] av = s.split("-");
String url = "http://www.youbian.com" + av[0];
String aa = ft.getName();
aa = aa.substring(0, aa.length() - 4);
f = new File(ROOT + p + "/" + aa);
if (!f.exists()) {
f.mkdirs();
}
f = new File(ROOT + p + "/" + aa, av[1] + ".txt");
if (f.exists()) {
continue;
} else {
f = new File(ROOT + p + "/" + aa, av[1] + ".abs");
if (f.exists()) {
f.delete();
}
}
f.createNewFile();
fos = new FileOutputStream(f);
parsejiedao(url, fos);
// 设置重命名规则
File f_new = new File(ROOT + p + "/" + aa, av[1] + ".txt");
f.renameTo(f_new);
}
}
}
public static void parsejiedao(String url, FileOutputStream fos2) {
try {
String t_url = "";
int num = 2;
int len = 0;
for (int id = 0; id < num; id++) {
if (id > 0) {
t_url = url + (id + 1) + ".htm";
// t_url="http://www.youbian.com/p511724/17.htm";
} else {
t_url = url;
}
// System.out.println(t_url);
// t_url="http://www.youbian.com/p511724/2.htm";
String data = getNet(t_url, "UTF-8");
if (data.length() == len) {
break;
}
len = data.length();
File f = new File("c:/a.txt");
if (f.exists()) {
f.delete();
}
FileOutputStream fos = new FileOutputStream("c:/a.txt");
fos.write(data.getBytes());
Pattern pattern = Pattern.compile(pa_jiedao);
Pattern pattern_code = Pattern.compile(pa_code);
Pattern pattern_qucode = Pattern.compile(pa_qucode);
Pattern pattern_num = Pattern.compile(pa_num);
BufferedReader in;
in = new BufferedReader(new FileReader("c:/a.txt"));
String s = null;
int flag = 0;
String qu = "";
String code = "";
String qucode = "";
while ((s = in.readLine()) != null) {
Matcher matcher = pattern_num.matcher(s);
if (matcher.find()) {
num = Integer.parseInt(matcher.group(2).trim());
}
matcher = pattern.matcher(s);
if (flag == 0 && matcher.find()) {
qu = matcher.group(3);
flag = 1;
} else if (flag == 1) {
matcher = pattern_code.matcher(s);
if (matcher.find()) {
code = matcher.group(3);
flag = 2;
}
} else if (flag == 2) {
matcher = pattern_qucode.matcher(s);
if (matcher.find()) {
qucode = matcher.group(1);
flag = 0;
System.out.println(qu + "----" + code + "----"
+ qucode);
fos2.write((qu.trim() + "-" + code.trim() + "-"
+ qucode.trim() + "\n").getBytes());
}
}
}
Thread.sleep(60);
}
fos2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void parsequxian(String url, String p) {
try {
String data = getNet(url, "UTF-8");
// System.out.println(data);
File f = new File(ROOT + p);
if (!f.exists()) {
f.mkdirs();
}
f = new File("c:/a.txt");
if (f.exists()) {
f.delete();
}
FileOutputStream fos = new FileOutputStream("c:/a.txt");
fos.write(data.getBytes());
Pattern pattern = Pattern.compile(pa_shi);
Pattern pattern_xian = Pattern.compile(pa_xian);
BufferedReader in;
in = new BufferedReader(new FileReader("c:/a.txt"));
String s = null;
boolean isxian = true;
while ((s = in.readLine()) != null) {
if (isxian) {
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
String bs = matcher.group(3);
// System.out.println(bs);
String a = bs.substring(0, bs.length() - 2);
f = new File(ROOT + p + "/", a + ".txt");
if (f.exists()) {
continue;
}
f.createNewFile();
fos = new FileOutputStream(f);
isxian = false;
fos.write((matcher.group(1) + "\n").getBytes());
}
} else {
Matcher matcher = pattern_xian.matcher(s);
if (matcher.find()) {
String a1 = matcher.group(1);
String a3 = matcher.group(3);
// System.out.println("---" + a1 + "----" + a3);
fos.write((a1 + "-" + a3 + "\n").getBytes());
} else {
Matcher matcher2 = pattern.matcher(s);
if (matcher2.find()) {
String bs = matcher2.group(3);
// System.out.println("xxx---" + bs);
// isxian = false;
String a = bs.substring(0, bs.length() - 2);
f = new File(ROOT + p + "/", a + ".txt");
if (f.exists()) {
f.delete();
}
f.createNewFile();
fos = new FileOutputStream(f);
fos.write((matcher2.group(1) + "\n").getBytes());
}
}
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getNet(String path, String chartype) {
try {
URL url = new URL(/blog_article/path/index.html);
URLConnection conn = url.openConnection();
// conn.setDoOutput(true);
conn.setDoInput(true);
conn.setReadTimeout(60000);
InputStream inputStream = conn.getInputStream();
String gg = conn.getHeaderField("Content-Encoding");
GZIPInputStream gzipInputStream;
InputStreamReader isr = null;
if (gg == null || "".equals(gg)) {
isr = new InputStreamReader(inputStream, chartype);
} else if (gg.equals("gzip")) {
gzipInputStream = new GZIPInputStream(inputStream);
isr = new InputStreamReader(gzipInputStream, chartype);
} else {
isr = new InputStreamReader(inputStream, chartype);
}
StringBuffer sb = new StringBuffer();
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
sb.append("\n");
}
isr.close();
inputStream.close();
conn = null;
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String deal(String input) {
if(input==null){
return "";
}
if (input.contains("dongguan")) {
return "/c441900/";
} else if (input.contains("zhongshan")) {
return "/c442000/";
} else if (input.contains("jiyuan")) {
return "/c410881/";
} else if (input.contains("jiayuguan")) {
return "/c620200/";
} else if (input.contains("sanya")) {
return "/c460200/";
} else if (input.contains("wulanchabumeng")) {// 无邮编
return "";
}
return input;
}
}