2013年1月2日 星期三

app list with one click & long click


package com.example.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.Menu;
import java.util.ArrayList;
import java.util.HashMap;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String outt = "";

final PackageManager pm = getPackageManager();
// get a list of installed apps.
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);

/*
* for (ApplicationInfo packageInfo : packages) {
*
* outt=
* outt+packageInfo.loadLabel(getPackageManager()).toString()+"\n"; outt
* = outt + packageInfo.packageName + "\n"; outt = outt +
* pm.getLaunchIntentForPackage(packageInfo.packageName) +
* "\n--------------\n";
*
* }// the getLaunchIntentForPackage returns an intent that you can use
* // with startActivity()
*/

// ListView "ListView1"
ListView list = (ListView) findViewById(R.id.ListView1);// 綁定Layout裡面的ListView
final List clickintent = new ArrayList();
final List pkn = new ArrayList();

// 生成動態數組,加入數據
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
for (ApplicationInfo packageInfo : packages) {
if (pm.getLaunchIntentForPackage(packageInfo.packageName) != null) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", packageInfo.loadIcon(getPackageManager()));// Photo
// ID
map.put("ItemTitle", packageInfo.loadLabel(getPackageManager())
.toString()); // Title
map.put("ItemText",
pm.getLaunchIntentForPackage(packageInfo.packageName)); // Content
map.put("ItemText",
pm.getLaunchIntentForPackage(packageInfo.packageName)); // Content
listItem.add(map);
clickintent.add(pm.getLaunchIntentForPackage(
packageInfo.packageName).toString());
pkn.add(packageInfo.packageName.toString());

}
}
// 生成適配器的Item和動態數組對應的元素
SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 數據源
R.layout.list_item,// ListItem的XML實現
// 動態數組與ImageItem對應的子項
new String[] { "ItemImage", "ItemTitle", "ItemText" },
// ImageItem的XML文件裡面的一個ImageView,兩個TextView ID
new int[] { R.id.ItemImage, R.id.ItemTitle, R.id.ItemText });

// Add and Show
list.setAdapter(listItemAdapter);

// One Click
list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// One Click To-Do
// Write File"clciked" to Internal Storage
String fileName = "clicked";
String content = (String) clickintent.get(arg2);
FileOutputStream writer = null;
try {
writer = openFileOutput(fileName, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.write(content.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// End of Write File"clciked" to Internal Storage
// Write File"pkn" to Internal Storage
String fileName2 = "pkn";
String content2 = (String) pkn.get(arg2);
FileOutputStream writer2 = null;
try {
writer2 = openFileOutput(fileName2, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer2.write(content2.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// End of Write File"pkn" to Internal Storage
}
});

// 添加長按點擊
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// Long Press To-Do
// Read in. st. from File"pkn"
String fileName = "pkn";
int readed;
String content = "";
byte[] buff = new byte[256]; // input stream buffer
// Input stream
try {
FileInputStream reader = openFileInput(fileName);
// FileInputStream reader =
// context.getApplicationContext().openFileInput(fileName);
while ((readed = reader.read(buff)) != -1) {
content += new String(buff).trim();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// End of Read in. st. from File"pkn"
// Read in. st. from File"clicked"
String fileName2 = "clicked";
int readed2;
String content2 = "";
byte[] buff2 = new byte[256]; // input stream buffer
// Input stream
try {
FileInputStream reader2 = openFileInput(fileName2);
// FileInputStream reader =
// context.getApplicationContext().openFileInput(fileName);
while ((readed2 = reader2.read(buff2)) != -1) {
content2 += new String(buff2).trim();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// End of Read in. st. from File"clicked"
Toast.makeText(getApplicationContext(), content,
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), content2,
Toast.LENGTH_SHORT).show();

Intent intent = new Intent("android.intent.action.MAIN");
intent.setPackage(content);
startActivity(intent);

}
});
// End of ListView "ListView1"

// Write File"test" to Internal Storage
String fileName = "test";
String content = outt;
FileOutputStream writer = null;
try {
writer = openFileOutput(fileName, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.write(content.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// End of Write File"test" to Internal Storage
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

app listview with one clcik


package com.example.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.Menu;
import java.util.ArrayList;
import java.util.HashMap;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String outt = "";

final PackageManager pm = getPackageManager();
// get a list of installed apps.
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);

/*
* for (ApplicationInfo packageInfo : packages) {
*
* outt=
* outt+packageInfo.loadLabel(getPackageManager()).toString()+"\n"; outt
* = outt + packageInfo.packageName + "\n"; outt = outt +
* pm.getLaunchIntentForPackage(packageInfo.packageName) +
* "\n--------------\n";
*
* }// the getLaunchIntentForPackage returns an intent that you can use
* // with startActivity()
*/

// ListView "ListView1"
ListView list = (ListView) findViewById(R.id.ListView1);// 綁定Layout裡面的ListView
final List clickintent=new ArrayList();

// 生成動態數組,加入數據
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
for (ApplicationInfo packageInfo : packages) {
if (pm.getLaunchIntentForPackage(packageInfo.packageName) != null) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", packageInfo.loadIcon(getPackageManager()));// Photo
// ID
map.put("ItemTitle", packageInfo.loadLabel(getPackageManager())
.toString()); // Title
map.put("ItemText",
pm.getLaunchIntentForPackage(packageInfo.packageName)); // Content
map.put("ItemText",
pm.getLaunchIntentForPackage(packageInfo.packageName)); // Content
listItem.add(map);
clickintent.add(packageInfo.loadLabel(getPackageManager())
.toString());
}
}
// 生成適配器的Item和動態數組對應的元素
SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 數據源
R.layout.list_item,// ListItem的XML實現
// 動態數組與ImageItem對應的子項
new String[] { "ItemImage", "ItemTitle", "ItemText" },
// ImageItem的XML文件裡面的一個ImageView,兩個TextView ID
new int[] { R.id.ItemImage, R.id.ItemTitle, R.id.ItemText });

// Add and Show
list.setAdapter(listItemAdapter);

// One Click
list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// One Click To-Do
//clickintent.get(arg2);
//Write File"clciked" to Internal Storage
String fileName = "clciked";
       String content=(String)clickintent.get(arg2);
       FileOutputStream writer = null;
 try {
  writer = openFileOutput(fileName, Context.MODE_PRIVATE);
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
       try {
  writer.write(content.getBytes());
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
       try {
  writer.close();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
//End of Write File"clciked" to Internal Storage
}
});

// 添加長按點擊
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// Long Press To-Do
}
});
// End of ListView "ListView1"

// Write File"test" to Internal Storage
String fileName = "test";
String content = outt;
FileOutputStream writer = null;
try {
writer = openFileOutput(fileName, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.write(content.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// End of Write File"test" to Internal Storage
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

android小知识:ArrayList和数组之间的转换


 

android小知识:ArrayList和数组之间的转换

分类: 技术文章 7330人阅读 评论(1) 收藏 举报
List----->数组
开发中不免碰到List与数组类型之间的相互转换,举一个简单的例子:
package test.test1;
import java.util.ArrayList;
import java.util.List;
public class Test {

/**
* @param args
*/
public static void main(String[] args) {
List list=new ArrayList();
list.add("王利虎");
list.add("张三");
list.add("李四");
int size=list.size();
String[] array=new String[size];
for(int i=0;i<list.size();i++){
array[i]=(String)list.get(i);
}
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}

app listview


package com.example.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.Menu;
import java.util.ArrayList;
import java.util.HashMap;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String outt = "";

final PackageManager pm = getPackageManager();
// get a list of installed apps.
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);

/*
* for (ApplicationInfo packageInfo : packages) {
*
* outt=
* outt+packageInfo.loadLabel(getPackageManager()).toString()+"\n"; outt
* = outt + packageInfo.packageName + "\n"; outt = outt +
* pm.getLaunchIntentForPackage(packageInfo.packageName) +
* "\n--------------\n";
*
* }// the getLaunchIntentForPackage returns an intent that you can use
* // with startActivity()
*/

// ListView "ListView1"
ListView list = (ListView) findViewById(R.id.ListView1);// 綁定Layout裡面的ListView

// 生成動態數組,加入數據
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
for (ApplicationInfo packageInfo : packages) {
if (pm.getLaunchIntentForPackage(packageInfo.packageName) != null) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", packageInfo.loadIcon(getPackageManager()));// Photo
// ID
map.put("ItemTitle", packageInfo.loadLabel(getPackageManager())
.toString()); // Title
map.put("ItemText",
pm.getLaunchIntentForPackage(packageInfo.packageName)); // Content
map.put("ItemText",
pm.getLaunchIntentForPackage(packageInfo.packageName)); // Content
listItem.add(map);
}
}
// 生成適配器的Item和動態數組對應的元素
SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 數據源
R.layout.list_item,// ListItem的XML實現
// 動態數組與ImageItem對應的子項
new String[] { "ItemImage", "ItemTitle", "ItemText" },
// ImageItem的XML文件裡面的一個ImageView,兩個TextView ID
new int[] { R.id.ItemImage, R.id.ItemTitle, R.id.ItemText });

// Add and Show
list.setAdapter(listItemAdapter);

// One Click
list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// One Click To-Do

}
});

// 添加長按點擊
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// Long Press To-Do
}
});
// End of ListView "ListView1"

// Write File"test" to Internal Storage
String fileName = "test";
String content = outt;
FileOutputStream writer = null;
try {
writer = openFileOutput(fileName, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.write(content.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// End of Write File"test" to Internal Storage
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

2013年1月1日 星期二

app list


String outt = "";

final PackageManager pm = getPackageManager();
// get a list of installed apps.
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {

outt= outt+packageInfo.loadLabel(getPackageManager()).toString()+"\n";
outt = outt + packageInfo.packageName + "\n";
outt = outt + pm.getLaunchIntentForPackage(packageInfo.packageName)
+ "\n--------------\n";

}// the getLaunchIntentForPackage returns an intent that you can use
// with startActivity()


-------------------------------------------------
http://stackoverflow.com/questions/2695746/how-to-get-a-list-of-installed-android-applications-and-pick-one-to-run

http://www.androidsnippets.com/get-installed-applications-with-name-package-name-version-and-icon

webview


WebView webview = (WebView)findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://i.wund.com/cgi-bin/findweather/getForecast?brand=iphone&query=hong+kong#conditions");

------------------------------------------------------------------------------

<uses-permission android:name="android.permission.INTERNET" />

Sound control

You can use the AudioManager class.
In this class you're looking for setRingerMode() function.
AudioManager audiomanage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audiomanage
.setRingerMode(AudioManager.RINGER_MODE_SILENT);
The values you can pass into the function are:
The ringer mode, one of RINGER_MODE_NORMALRINGER_MODE_SILENT, orRINGER_MODE_VIBRATE.
You have to add this into the manifest file:
android.permission.MODIFY_AUDIO_SETTINGS
share|improve this answer
Related Posts Plugin for WordPress, Blogger...