您的位置:首页 > 理论基础 > 计算机网络

整理与网络资源

2010-08-17 11:30 288 查看
整理与网络资源

Intent应该算是Android中特有的东西。你可以在Intent中指定程序要执行的动作(比如:view,edit,dial),以及程序执行到该动作时所需要的资料。都指定好后,只要调用startActivity(),Android系统会自动寻找最符合你指定要求的应用程序,并执行该程序。

打开其他APK文件的ACTIVITY

Intent mIntent = new Intent();
ComponentName comp = new ComponentName("com.android.a","com.android.a.AbcActivity" );
mIntent.setComponent(comp);
mIntent.setAction("android.intent.action.MAIN");
startActivity(mIntent);

显示网页:

1. Uri uri = Uri.parse("http://www.google.com");

2. Intent it = new Intent(Intent.ACTION_VIEW,uri);

3. startActivity(it);

显示地图:

1. Uri uri = Uri.parse("geo:38.899533,-77.036476");

2. Intent it = new Intent(Intent.Action_VIEW,uri);

3. startActivity(it);

路径规划:

1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");

2. Intent it = new Intent(Intent.ACTION_VIEW,URI);

3. startActivity(it);

拨打电话:
调用拨号程序

1. Uri uri = Uri.parse("tel:xxxxxx");

2. Intent it = new Intent(Intent.ACTION_DIAL, uri);

3. startActivity(it);

1. Uri uri = Uri.parse("tel.xxxxxx");

2. Intent it =new Intent(Intent.ACTION_CALL,uri);

3. 要使用这个必须在配置文件中加入<uses-permission android:name="android.permission.CALL_PHONE" />

发送SMS/MMS
调用发送短信的程序

1. Intent it = new Intent(Intent.ACTION_VIEW);

2. it.putExtra("sms_body", "The SMS text");

3. it.setType("vnd.android-dir/mms-sms");

4. startActivity(it);

发送短信

1. Uri uri = Uri.parse("smsto:0800000123");

2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);

3. it.putExtra("sms_body", "The SMS text");

4. startActivity(it);

发送彩信

1. Uri uri = Uri.parse("content://media/external/images/media/23");

2. Intent it = new Intent(Intent.ACTION_SEND);

3. it.putExtra("sms_body", "some text");

4. it.putExtra(Intent.EXTRA_STREAM, uri);

5. it.setType("image/png");

6. startActivity(it);

发送Email

1.

2. Uri uri = Uri.parse("mailto:xxx@abc.com");

3. Intent it = new Intent(Intent.ACTION_SENDTO, uri);

4. startActivity(it);

1. Intent it = new Intent(Intent.ACTION_SEND);

2. it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");

3. it.putExtra(Intent.EXTRA_TEXT, "The email body text");

4. it.setType("text/plain");

5. startActivity(Intent.createChooser(it, "Choose Email Client"));

1. Intent it=new Intent(Intent.ACTION_SEND);

2. String[] tos={"me@abc.com"};

3. String[] ccs={"you@abc.com"};

4. it.putExtra(Intent.EXTRA_EMAIL, tos);

5. it.putExtra(Intent.EXTRA_CC, ccs);

6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");

7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");

8. it.setType("message/rfc822");

9. startActivity(Intent.createChooser(it, "Choose Email Client"));

添加附件

1. Intent it = new Intent(Intent.ACTION_SEND);

2. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");

3. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");

4. sendIntent.setType("audio/mp3");

5. startActivity(Intent.createChooser(it, "Choose Email Client"));

播放多媒体

//方法1.

Uri uri = Uri.parse("file:///sdcard/song.mp3");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
// it.addFlags(it.FLAG_ACTIVITY_NEW_TASK);//非必须选项
it.setDataAndType(uri, "audio/mp3");
context.startActivity(it);

//方法2.

1.

2. Intent it = new Intent(Intent.ACTION_VIEW);

3. Uri uri = Uri.parse("file:///sdcard/song.mp3");

4. it.setDataAndType(uri, "audio/mp3");

5. startActivity(it);

//方法3. 启动一个播放器并播放一个系统声音

1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");

2. Intent it = new Intent(Intent.ACTION_VIEW, uri);

3. startActivity(it);

Uninstall 程序

1. Uri uri = Uri.fromParts("package", strPackageName, null);

2. Intent it = new Intent(Intent.ACTION_DELETE, uri);

3. startActivity(it);

uninstall apk

1. Uri uninstallUri = Uri.fromParts("package", "xxx", null);

2.

3. returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);

install apk

1. Uri installUri = Uri.fromParts("package", "xxx", null);

2.

3. returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

play audio

1. Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");

2.

3. returnIt = new Intent(Intent.ACTION_VIEW, playUri);

1. //发送附件
2. Intent it = new Intent(Intent.ACTION_SEND);

3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");

4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3");

5. sendIntent.setType("audio/mp3");

6. startActivity(Intent.createChooser(it, "Choose Email Client"));

market相关

再来一个market相关的:
market相关

1. //搜索应用

2. Uri uri = Uri.parse("market://search?q=pname:pkg_name");

3. Intent it = new Intent(Intent.ACTION_VIEW, uri);

4. startActivity(it);

5. //where pkg_name is the full package path for an application

6.

7. //显示指定应用的详细页面(这个好像不支持了,找不到app_id)

8. Uri uri = Uri.parse("market://details?id=app_id");

9. Intent it = new Intent(Intent.ACTION_VIEW, uri);

10. startActivity(it);

11. //where app_id is the application ID, find the ID

12. //by clicking on your application on Market home

13. //page, and notice the ID from the address bar

下面列出几种Intent的用法

1:查看是否有存储卡插入

String status=Environment.getExternalStorageState();
if(status.equals(Enviroment.MEDIA_MOUNTED))
{
说明有SD卡插入
}

OnCreate中不设Layout

this.setTheme(R.style.Theme_Transparent);

以下是Theme_Transparent的定义(注意transparent_bg是一副透明的图片)

编辑本段 回目录3:在屏幕元素中设置句柄

使用Activity.findViewById来取得屏幕上的元素的句柄. 使用该句柄您可以设置或获取任何该对象外露的值.
TextView msgTextView = (TextView)findViewById(R.id.msg);
msgTextView.setText(R.string.push_me);

String body=”this is mms demo”;

Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”smsto”, number, null));
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
startActivity(mmsintent);

StringBuilder sb = new StringBuilder();

sb.append(”file://”);

sb.append(fd.getAbsoluteFile());

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(”mmsto”, number, null));
// Below extra datas are all optional.
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);

startActivity(intent);

mime = “img/jpg”;
shareIntent.setDataAndType(Uri.fromFile(fd), mime);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fd));
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

shareIntent.putExtra(Intent.EXTRA_TEXT, body);

registerReceiver(mMasterResetReciever, new IntentFilter(”OMS.action.MASTERRESET”));

private BroadcastReceiver mMasterResetReciever = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if(”oms.action.MASTERRESET”.equals(action)){
RecoverDefaultConfig();
}
}

};

private ContentObserver mDownloadsObserver = new DownloadsChangeObserver(Downloads.CONTENT_URI);

private class DownloadsChangeObserver extends ContentObserver {
public DownloadsChangeObserver(Uri uri) {
super(new Handler());

}

@Override
public void onChange(boolean selfChange) {}
}

public String getUserAgent()
{
String user_agent = ProductProperties.get(ProductProperties.USER_AGENT_KEY, null);
return user_agent;
}

CookieSyncManager.createInstance(getApplicationContext());
CookieManager.getInstance().removeAllCookie();

//Dial the GPRS link.
private boolean openDataConnection() {
// Set up data connection.
DataConnection conn = DataConnection.getInstance();

if (connectMode == 0) {
ret = conn.openConnection(mContext, “cmwap”, “cmwap”, “cmwap”);
} else {
ret = conn.openConnection(mContext, “cmnet”, “”, “”);
}

}

public class Setting extends PreferenceActivity



public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}



Setting.xml:

Android:key=”seting2″
android:title=”@string/seting2″
android:summary=”@string/seting2″/>

android:key=”seting1″
android:title=”@string/seting1″
android:summaryOff=”@string/seting1summaryOff”
android:summaryOn=”@stringseting1summaryOff”/>

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet method = new HttpGet(“http://www.baidu.com/1.html”);
HttpResponse resp;
Reader reader = null;
try {
// AllClientPNames.TIMEOUT
HttpParams params = new BasicHttpParams();
params.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT, 10000);
httpClient.setParams(params);
resp = httpClient.execute(method);
int status = resp.getStatusLine().getStatusCode();

if (status != HttpStatus.SC_OK) return false;

// HttpStatus.SC_OK;
return true;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

编辑本段 回目录14:显示toast

Toast.makeText(this._getApplicationContext(), R.string._item, Toast.LENGTH_SHORT).show();

编辑本段 回目录9:获得 手机UA

15. android 彻底关闭应用程序(back按键)

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

//按下键盘上返回按钮
if(keyCode == KeyEvent.KEYCODE_BACK){
finish();
return true;
}else{
return super.onKeyDown(keyCode, event);
}
}

@Override
protected void onDestroy() {
super.onDestroy();

System.exit(0);
//或者下面这种方式
//android.os.Process.killProcess(android.os.Process.myPid());
}
16.像素设置单位简单了解

Px (pixels)像素, 屏幕上的点
Dip,dp(device independent pixels)依赖于设备的像素, 与密度无关的像素, 一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp = 1px。 dip:与dp相同,多用于Google示例中
Sp(scaled pixels—best for text size)带比例的像素, 与刻度无关的像素):与dp类似,但是可以根据用户的字体大小首选项进行缩放
Pt(points)点, 1/72英寸
In(inches)英尺, 长度单位
mm(millimeters)毫米,长度单位
其中最常用的是px和dip,尽量使用dp作为空间大小单位,sp作为和文字相关大小单位

17.Android开发UI工具

文章分类:JavaEye

http://code.google.com/p/droiddraw/downloads/list这个地址有所有版本的可以下载;

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kome2000/archive/2010/07/13/5730981.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: