1.成为分享应用程序
什么是分享功能,比如说可以把你的自拍照片传到微博上面。如下图所示,所有可以共享图片的应用列表:
代码如何实现呢 Manifest定义的Activity 如下所示:
<activity android:name=".SendActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
Activity 的action必须是:android.intent.action.SEND,android:mimeType 可以是多种类型如:“image/*”是所有类型的图片,“image/png”是指png格式的图片,“*/*指所有类型的文件:包括图片,视频,音频等等。
Activity 的简单代码如下:
Intent intent = getIntent();
String action = intent.getAction();
Bundle extras = intent.getExtras();
// if this is from the share menu 如果它来自点击共享按钮之后,选择我们的应用“UpdateTest” 之后就可以显示下面的Activity
if (Intent.ACTION_SEND.equals(action))
{
if (extras.containsKey(Intent.EXTRA_STREAM))
{
try
{
// Get resource path from intent callee 传递过来的url
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
imageview.setImageURI(uri);
System.out.println("uri toString()-------" + uri.toString());
return;
} catch (Exception e)
{
// Log.e(this.getClass().getName(), e.toString());
}
} else if (extras.containsKey(Intent.EXTRA_TEXT))
{
return;
}
}
上面的Activity仅是简单显示要共享的图片,之后可以把这张图片传递到你的服务器就OK了。
2.成为增加账户的应用程序
效果图如下:
Addaccount是我的具有账户功能的应用程序。
Manifest的配置如下:
<service
android:name=".MyServiceName"
android:exported="false" >
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticatior" />
</service>
authenticatior 的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="myType"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:smallIcon="@drawable/ic_launcher" />
账户应用程序链接之后可以对设备进行管理,对设备进行监控等等。