Andriod应用开发(例子)
日志编号:133 发表时间: 2012-10-21 22:25:18 关注次数:2942放着,备着先,空了整理写一文章。
获取数据:
以下为代码内容,请直接复制粘贴:
点击启动新页面(窗体):
以下为代码内容,请直接复制粘贴:
传递数据到新也面(窗体):
以下为代码内容,请直接复制粘贴:
在show.java页面使用String hid=this.getIntent().getStringExtra("id");//接收
解析json数据:
以下为代码内容,请直接复制粘贴:
检查版本(根据以上的几段代码,可以从服务器中读取json信息与app版本对照即可):
以下为代码内容,请直接复制粘贴:
下载AXMLPrinter2.jar:http://code.google.com/p/android4me/downloads/detail?name=AXMLPrinter2.jar&can=2&q=放C盘或某目录都行,并创建axml.cmd文件,写入java -jar C:\AXMLPrinter2.jar %1 > %2,解压下载的apk文件,提取其中的xml文件,在命令行输入:c:\axml.cmd AndroidManifest.xml AndroidManifest.xml.txt就可以得到代码了。提醒:注意路径
dex2jar反编译:http://code.google.com/p/dex2jar/ (如果打不开,则需要翻墙),将apk文件解压后,拖动classes.dex文件到dex2jar下的dex2jar.bat即可生成classes_dex2jar.jar。或命令行输入:dex2jar.bat classes.dex
使用JD-GUI打开classes_dex2jar.jar:http://java.decompiler.free.fr/?q=jdgui
获取数据:
以下为代码内容,请直接复制粘贴:
public void gethttp(int offset)
{
try{
URL myURL = new URL("http://www.a.com/listview.php?offset="+offset);
URLConnection conn = myURL.openConnection();
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line + "\n");
}
in.close();
tmp=sb.toString();
}catch(Exception e){
if(offset > 0){
Toast.makeText(this, "无法读取远程数据,请检查网络情况。",Toast.LENGTH_SHORT).show();
}
else
{TextView textgetnone=(TextView) findViewById(R.id.getnone);
textgetnone.setVisibility(0);
textgetnone.setText("无法读取远程数据,请检查网络情况。");
listView.setVisibility(8);
}
}
}
{
try{
URL myURL = new URL("http://www.a.com/listview.php?offset="+offset);
URLConnection conn = myURL.openConnection();
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = in.readLine()) != null) {
sb.append(line + "\n");
}
in.close();
tmp=sb.toString();
}catch(Exception e){
if(offset > 0){
Toast.makeText(this, "无法读取远程数据,请检查网络情况。",Toast.LENGTH_SHORT).show();
}
else
{TextView textgetnone=(TextView) findViewById(R.id.getnone);
textgetnone.setVisibility(0);
textgetnone.setText("无法读取远程数据,请检查网络情况。");
listView.setVisibility(8);
}
}
}
点击启动新页面(窗体):
以下为代码内容,请直接复制粘贴:
public void onclick_abc(View view) throws Exception{//button或textviewid为onclice_abc
Intent intent = new Intent();
intent.setClass(this,efg.class);//对应创建文件efg.xml和efg.java
startActivity(intent);
}
Intent intent = new Intent();
intent.setClass(this,efg.class);//对应创建文件efg.xml和efg.java
startActivity(intent);
}
传递数据到新也面(窗体):
以下为代码内容,请直接复制粘贴:
Intent intent = new Intent();
intent.putExtra("id",id);//传递id参数到show.java
intent.setClass(MainActivity.this,show.class);
startActivity(intent);
intent.putExtra("id",id);//传递id参数到show.java
intent.setClass(MainActivity.this,show.class);
startActivity(intent);
在show.java页面使用String hid=this.getIntent().getStringExtra("id");//接收
解析json数据:
以下为代码内容,请直接复制粘贴:
try{
JSONArray jArray = new JSONArray(tmp);//tmp为json数据变量
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
// Iterator<?> keys=json_data.keys();
// while(keys.hasNext()){
// result += json_data.getString(keys.next().toString());
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("title",json_data.getString("title"));
hashMap.put("content",json_data.getString("content"));
listData.add(hashMap);
// }
}
}catch(Exception e){
//e.printStackTrace();
}
JSONArray jArray = new JSONArray(tmp);//tmp为json数据变量
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
// Iterator<?> keys=json_data.keys();
// while(keys.hasNext()){
// result += json_data.getString(keys.next().toString());
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("title",json_data.getString("title"));
hashMap.put("content",json_data.getString("content"));
listData.add(hashMap);
// }
}
}catch(Exception e){
//e.printStackTrace();
}
检查版本(根据以上的几段代码,可以从服务器中读取json信息与app版本对照即可):
以下为代码内容,请直接复制粘贴:
JSONTokener jsonParser = new JSONTokener(tmp);
JSONObject apk = (JSONObject) jsonParser.nextValue();
// apk.getString("lastversion");
// Toast.makeText(this,apk.getString("agent"), Toast.LENGTH_SHORT).show();
if(apk.getString("lastversion").equals(Function.getVersionName(this))==false)//Function.getVersionName(this)为存放在Funcion.java文件里的一个函数getVersionName(),用以读取当前app版本,app版本设置在AndroidManifest的android:versionName="0.3.2"
{ Dialog dialog = new AlertDialog.Builder(this).setTitle("系统更新")//对话框标题
//dialog.setIcon(getResources().getDrawable(R.drawable.icon));
.setMessage("发现新版本,大小"+apk.getString("appsize")+"K,版本:"+apk.getString("lastversion")+"。请更新!")// 设置内容。从json文件中读取数据
.setPositiveButton("马上更新",// 设置确定按钮
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// pBar = new ProgressDialog(about.this);
// pBar.setTitle("正在下载");
// pBar.setMessage("请稍候...");
// pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// fileEx = strURL.substring(strURL.lastIndexOf(".")+1,strURL.length()).toLowerCase();
// fileNa = strURL.substring(strURL.lastIndexOf("/")+1,strURL.lastIndexOf("."));
// getFile(strURL);
Uri uri = Uri.parse(Config.Apk_url); //这里采用弹出浏览器下载
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
}
}).setNegativeButton("以后再说",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
}
}).create();// 创建
// 显示对话框
dialog.show();
}
else
{Toast.makeText(this, "木有新版本!", Toast.LENGTH_SHORT).show();
}
JSONObject apk = (JSONObject) jsonParser.nextValue();
// apk.getString("lastversion");
// Toast.makeText(this,apk.getString("agent"), Toast.LENGTH_SHORT).show();
if(apk.getString("lastversion").equals(Function.getVersionName(this))==false)//Function.getVersionName(this)为存放在Funcion.java文件里的一个函数getVersionName(),用以读取当前app版本,app版本设置在AndroidManifest的android:versionName="0.3.2"
{ Dialog dialog = new AlertDialog.Builder(this).setTitle("系统更新")//对话框标题
//dialog.setIcon(getResources().getDrawable(R.drawable.icon));
.setMessage("发现新版本,大小"+apk.getString("appsize")+"K,版本:"+apk.getString("lastversion")+"。请更新!")// 设置内容。从json文件中读取数据
.setPositiveButton("马上更新",// 设置确定按钮
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// pBar = new ProgressDialog(about.this);
// pBar.setTitle("正在下载");
// pBar.setMessage("请稍候...");
// pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// fileEx = strURL.substring(strURL.lastIndexOf(".")+1,strURL.length()).toLowerCase();
// fileNa = strURL.substring(strURL.lastIndexOf("/")+1,strURL.lastIndexOf("."));
// getFile(strURL);
Uri uri = Uri.parse(Config.Apk_url); //这里采用弹出浏览器下载
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
}
}).setNegativeButton("以后再说",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
}
}).create();// 创建
// 显示对话框
dialog.show();
}
else
{Toast.makeText(this, "木有新版本!", Toast.LENGTH_SHORT).show();
}
下载AXMLPrinter2.jar:http://code.google.com/p/android4me/downloads/detail?name=AXMLPrinter2.jar&can=2&q=放C盘或某目录都行,并创建axml.cmd文件,写入java -jar C:\AXMLPrinter2.jar %1 > %2,解压下载的apk文件,提取其中的xml文件,在命令行输入:c:\axml.cmd AndroidManifest.xml AndroidManifest.xml.txt就可以得到代码了。提醒:注意路径
dex2jar反编译:http://code.google.com/p/dex2jar/ (如果打不开,则需要翻墙),将apk文件解压后,拖动classes.dex文件到dex2jar下的dex2jar.bat即可生成classes_dex2jar.jar。或命令行输入:dex2jar.bat classes.dex
使用JD-GUI打开classes_dex2jar.jar:http://java.decompiler.free.fr/?q=jdgui
本站不提供讨论功能。
本站所有非新闻类文章均为原创,且禁止转载。
本站为了获得更多流量赚取广告费,难免会有以次充好的文章,望见谅,勿鄙视。
本站所有非新闻类文章均为原创,且禁止转载。
本站为了获得更多流量赚取广告费,难免会有以次充好的文章,望见谅,勿鄙视。