当前位置:  编程技术>移动开发
本页文章导读:
    ▪透过NSNotificationCenter更改被选中的背景色        通过NSNotificationCenter更改被选中的背景色 1,通过NSNotificationCenter注册监听事件   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuWillShow:) .........
    ▪ 【转】Service怎么关闭启动自己的Activity        【转】Service如何关闭启动自己的Activity 今天在编写Android程序的时候,遇到这个问题。通过百度和google的搜索结果,总结出以下方法:方法一:public class mService extends Service {//保存在service中的.........
    ▪ 获取全国邮政编码       获取全国邮编 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..........

[1]透过NSNotificationCenter更改被选中的背景色
    来源: 互联网  发布时间: 2014-02-18
通过NSNotificationCenter更改被选中的背景色

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];
}
 

    
[2] 【转】Service怎么关闭启动自己的Activity
    来源: 互联网  发布时间: 2014-02-18
【转】Service如何关闭启动自己的Activity

今天在编写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();
……
}
}


    
[3] 获取全国邮政编码
    来源: 互联网  发布时间: 2014-02-18
获取全国邮编
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;
}

}

    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3