参考:
http://www.chilkatsoft.com/java-mht.asp
http://www.example-code.com/java/javamht.asp
http://www.javaeye.com/topic/177504#503532
2)
Android WebKit 简单实例
http://dev.10086.cn/cmdn/wiki/index.php?doc-view-2213.html
3)
private static final String FOLDER_PATH = "/data/data/com.ziilabs.wheel/files/";
FOLDER_PATH + "wheel.ms3d"
4)loadRes
public void loadRes() {
File folder = new File(FOLDER_PATH);
if( !folder.exists() ) {
folder.mkdir();
int res[] = {R.raw.wheel, R.raw.belt, R.raw.inner_ring, R.raw.middle_ring, R.raw.outer_ring,R.raw.side};
InputStream in[] = new InputStream[res.length];
File file[] = new File[res.length];
for (int i = 0; i < res.length; i++) {
in[i] = getResources().openRawResource(res[i]);
file[i] = new File(MODELS[i]);
try {
FileOutputStream fos = new FileOutputStream(file[i]);
byte buff[] = new byte[1024];
do {
int numread = in[i].read(buff);
if(numread <= 0) {
break;
}
fos.write(buff,0,numread);
}
while(true);
in[i].close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5 项目总结:
a) 寻找系统注册过的Activity,根据category
public static GamePlugin findFirst(Context context, String category) {
Intent pluginIntent = new Intent(Intent.ACTION_MAIN, null);
pluginIntent.addCategory(category);
List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(pluginIntent, PackageManager.GET_PROVIDERS);
if (apps.isEmpty())
return null;
for (ResolveInfo info : apps) {
ActivityInfo activityInfo = info.activityInfo;
PackageInfo pkgInfo = null;
try {
pkgInfo = context.getPackageManager().getPackageInfo(activityInfo.packageName, PackageManager.GET_PROVIDERS);
}
catch (Exception ex) {
continue;
}
// now get "pkgInfo.versionName" and "pkgInfo.packageName" and "activityInfo.name"
//TODO : save the game info to the list
GamePlugin plugin = new GamePlugin();
plugin.setGameVersion(pkgInfo.versionName);
plugin.setGamePackage( pkgInfo.packageName);
plugin.setGameActivity( activityInfo.name);
plugin.setGameType(GamePlugin.TYPE_UNKNOWN);
// GamePlugin plugin = new GamePlugin();
// plugin.version = pkgInfo.versionName;
// plugin.setNames(pkgInfo.packageName, activityInfo.name);
// plugin.setContentType(PluginType.unknown);
// if (pkgInfo.providers != null && pkgInfo.providers.length > 0)
// plugin.setContentProviderAuthority(pkgInfo.providers[0].authority);
return plugin; // just return the first one.
}
return null;
}
b)链表操作
申请一个链表
List<GamePlugin> matches = new ArrayList<GamePlugin>();
查找链表
//find game in list
//find game in list
//find game in list
public boolean find(String str) {
if (matches == null) return false;
for(int i = 0;i < matches.size(); i++)
{
if(((String)matches.get(i)).equals(str))//假设你的NAME是String类型的
{
//此时的list.get(i)就是你要的元素
}
}
return true;
}
排序链表
int index = -1;
GamePlugin pluginInfo = new GamePlugin();
//magicphrase
index = findName(MAGIC_PHRASE);
if (index >= 0) {
pluginInfo = pluginList.get(index);
GamePlugin pluginPhrase = new GamePlugin();
pluginPhrase.setGameVersion(pluginInfo.GetGameVersion());
pluginPhrase.setGamePackage( pluginInfo.GetGamePackage());
pluginPhrase.setGameActivity( pluginInfo.GetGameActivity());
pluginPhrase.setGameType(GamePlugin.TYPE_MAGIC_CHANGE_CORLOR);
pluginList.remove(index);
pluginList.add(0, pluginPhrase);
}
//magicdraw
index = findName(MAGIC_DETECTOR);
if (index >= 0) {
pluginInfo = pluginList.get(index);
GamePlugin pluginDraw = new GamePlugin();
pluginDraw.setGameVersion(pluginInfo.GetGameVersion());
pluginDraw.setGamePackage( pluginInfo.GetGamePackage());
pluginDraw.setGameActivity( pluginInfo.GetGameActivity());
pluginDraw.setGameType(GamePlugin.TYPE_MAGIC_CHANGE_CORLOR);
pluginList.remove(index);
pluginList.add(0, pluginDraw);
}
NOTE:注意pluginPhrase 和 pluginDraw不能使用同一个变量,要不然
Original:L-->MAGIC_DETECTOR--->MAGIC_PHRASE,
First: L--->MAGIC_PHRASE---->MAGIC_DETECTOR
Second L-->MAGIC_DETECTOR--->MAGIC_DETECTOR (不对了)
java申请的内存相当与指针,相当于MAGIC_DETECTOR 同一地址被加载了两次了
所有要用不同的变量申明
c)学会了创建OptionsMenuItem,重载以下几个函数
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.v("@@@@", "enter: " + "onKeyDown" + " ====> you pressed key: " + String.valueOf(keyCode));
return super.onKeyDown(keyCode, event);
}
// 点击Menu时,系统调用当前Activity的onCreateOptionsMenu方法,并传一个实现了一个Menu接口的menu对象供你使用
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*
* add()方法的四个参数,依次是:
* 1、组别,如果不分组的话就写Menu.NONE,
* 2、Id,这个很重要,Android根据这个Id来确定不同的菜单
* 3、顺序,那个菜单现在在前面由这个参数的大小决定
* 4、文本,菜单的显示文本
*/
Log.v("@@@@", "enter: " + "onCreateOptionsMenu");
menu.add(Menu.NONE, Menu.FIRST + 3, 1, "帮助").setIcon(
android.R.drawable.ic_menu_help);
// menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon(
// android.R.drawable.ic_menu_delete);
// // setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以
// // android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的
// menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon(
// android.R.drawable.ic_menu_edit);
// menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon(
// android.R.drawable.ic_menu_help);
// menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon(
// android.R.drawable.ic_menu_add);
// menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon(
// android.R.drawable.ic_menu_info_details);
// menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon(
// android.R.drawable.ic_menu_send);
// return true才会起作用
return true;
}
//菜单项被选择事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.v("@@@@", "enter: " + "onOptionsItemSelected");
switch (item.getItemId()) {
case Menu.FIRST + 1:
Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 2:
Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
//Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show();
//TODO open mht document
break;
case Menu.FIRST + 4:
Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 5:
Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show();
break;
}
return false;
}
//选项菜单被关闭事件,菜单被关闭有三种情形,menu按钮被再次点击、back按钮被点击或者用户选择了某一个菜单项
@Override
public void onOptionsMenuClosed(Menu menu){
Log.v("@@@@", "enter: " + "onOptionsMenuClosed");
}
//菜单被显示之前的事件
@Override
public boolean onPrepareOptionsMenu(Menu menu){
Log.v("@@@@", "enter: " + "onOptionsMenuClosed");
//如果返回false,此方法就把用户点击menu的动作给消费了,onCreateOptionsMenu方法将不会被调用
return true;
}
d)打开本地.html或者.mht 格式的文件 两种方法
第一种:启动浏览器应用,不同的Activity
//http:
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_uri_browsers = Uri.parse("http://csdn.net"); //OK
intent.setData(content_uri_browsers);
intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
startActivity(intent);
//在SD卡上的文件
Uri content_uri_browsers = Uri.parse("content://com.android.htmlfileprovider/sdcard/magicware_userguide.mht"); //OK
//在应用程序能访问的目录: //ERROR
static String GUIDE_NAME = "magicware_userguide.mht";
static String URI_PATH = "/data/data/com.ziilabs.magicwares/files/";
Uri content_uri_browsers = Uri.parse("file://" + URI_PATH + GUIDE_NAME);
第二种 用import android.webkit.WebView
WebView webView1 = new WebView(this);
// webView1
// .loadData(
// "<html><body>Can I focus?<br /><a href=/"#/">No I cannot!</a>.</body></html>",
// "text/html", "utf-8");
//webView1.loadUrl("http://csdn.net"); //OK
webView1.loadUrl("file://" + URI_PATH + GUIDE_NAME); //OK
//webView1.loadUrl("content://com.android.htmlfileprovider/sdcard/magicware_userguide.mht"); //OK
this.setContentView(webView1);
//在应用程序能访问的目录:
static String GUIDE_NAME = "magicware_userguide.mht";
static String URI_PATH = "/data/data/com.ziilabs.magicwares/files/";
Uri content_uri_browsers = Uri.parse("file://" + URI_PATH + GUIDE_NAME);
注意:在这种时候需要用WebView来实现,不同的Activity访问文件的权限问题决定的
e)loadRes,应用程序例如com.android.test, 可以写入资源文件的路径为
/data/data/com.android.test/yourmakedir/filename.txt
对SD卡进行读写还要进行设置,如:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public void loadRes()
{
File folder = new File(URI_PATH);
if( !folder.exists() ) {
Log.v("@@@@", "Dir: " + URI_PATH + " not exsit ~~~");
folder.mkdir();
if( !folder.exists() ) {
Log.v("@@@@", "Dir: " + URI_PATH + " create faild ~~~");
} else {
Log.v("@@@@", "Dir: " + URI_PATH + " create suc ~~~");
}
InputStream in = getResources().openRawResource(R.raw.magicware_userguide);
File file = new File(URI_PATH + GUIDE_NAME);
try {
FileOutputStream fos = new FileOutputStream(file);
byte buff[] = new byte[1024];
do
{
int numread = in.read(buff);
if(numread <= 0)
{
break;
}
fos.write(buff,0,numread);
}while(true);
in.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.v("@@@@", "Dir: " + URI_PATH + " FileNotFoundException ");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("@@@@", "Dir: " + URI_PATH + GUIDE_NAME + " file IOException");
e.printStackTrace();
}
}
else {
Log.v("@@@@", "Dir: " + URI_PATH + " aready exsit ~~~");
}
}
f)OnClick事件不要用for来实现OnClick中接收不了来自for循环里面的i,应该单独的写,
在OnCreate的时候事执行不了的,但是可以这样判断
if (buttonCount < 7) return; //这个地方返回了
m_Button[6].setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
//Do whatever you want
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
packageName = pluginList.get(6).GetGamePackage();
activityName = pluginList.get(6).GetGameActivity();
intent.setClassName(packageName, activityName);
startActivity(intent);
}
});
g)分解单词,此为得到最后的一个
public String GetActivityName(String str) {
int numWords;
StringTokenizer st;
//grab our string resource words
st = new StringTokenizer(str,".");
numWords = st.countTokens();
//Log.d(TAG,"count = " + numWords);
String []mItems = new String[numWords];
for(int i = 0; i < numWords; i++)
{
mItems[i] = st.nextToken();
}
return mItems[numWords-1];
}
H)
http://licongf18.blog.163.com/blog/static/155695482010102655746661/
I) 添加button背景图
layout2.addView(m_Button[i]);
m_Button[i].setBackgroundResource(R.drawable.button_bg);
m_Button[i].setTextSize(25f);
m_Button[i].setTextScaleX (1.0f);
m_Button[i].setPadding(0, 0, 25, 22);
button_bu.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_ontouch"
android:state_pressed="true" />
<item android:drawable="@drawable/button_default"
android:state_pressed="false" />
</selector>