Merge "New sample code for receiving a share and processing in a service."
This commit is contained in:
committed by
Android (Google) Code Review
commit
2876c72e34
16
samples/ReceiveShareDemo/Android.mk
Normal file
16
samples/ReceiveShareDemo/Android.mk
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
LOCAL_PATH:= $(call my-dir)
|
||||||
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
|
LOCAL_MODULE_TAGS := samples
|
||||||
|
|
||||||
|
# Only compile source java files in this apk.
|
||||||
|
LOCAL_SRC_FILES := $(call all-java-files-under, src)
|
||||||
|
|
||||||
|
LOCAL_PACKAGE_NAME := ReceiveShareDemo
|
||||||
|
|
||||||
|
LOCAL_SDK_VERSION := current
|
||||||
|
|
||||||
|
include $(BUILD_PACKAGE)
|
||||||
|
|
||||||
|
# Use the following include to make our test apk.
|
||||||
|
include $(call all-makefiles-under,$(LOCAL_PATH))
|
||||||
31
samples/ReceiveShareDemo/AndroidManifest.xml
Normal file
31
samples/ReceiveShareDemo/AndroidManifest.xml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2015 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="com.example.android.receiveshare">
|
||||||
|
<application android:label="@string/app_name">
|
||||||
|
<activity android:name=".ReceiveShare">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.SEND" />
|
||||||
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
<data android:mimeType="*/*" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
|
||||||
|
<service android:name=".ReceiveShareService" android:process=":service">
|
||||||
|
</service>
|
||||||
|
</application>
|
||||||
|
</manifest>
|
||||||
41
samples/ReceiveShareDemo/res/layout/receive_share.xml
Normal file
41
samples/ReceiveShareDemo/res/layout/receive_share.xml
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2015 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!-- Demonstrates selecting various types of contact data. -->
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:orientation="vertical" android:padding="4dip"
|
||||||
|
android:gravity="center_horizontal"
|
||||||
|
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent" android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="0"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
|
android:text="@string/receive_share_msg"/>
|
||||||
|
|
||||||
|
<TextView android:id="@+id/receive_share_data"
|
||||||
|
android:layout_width="match_parent" android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="0"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
|
android:paddingTop="8dip"
|
||||||
|
android:paddingBottom="8dip"/>
|
||||||
|
|
||||||
|
<Button android:id="@+id/send_to_service"
|
||||||
|
android:layout_width="wrap_content" android:layout_height="wrap_content"
|
||||||
|
android:text="@string/send_to_service">
|
||||||
|
<requestFocus />
|
||||||
|
</Button>
|
||||||
|
</LinearLayout>
|
||||||
23
samples/ReceiveShareDemo/res/values/strings.xml
Normal file
23
samples/ReceiveShareDemo/res/values/strings.xml
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2015 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
|
||||||
|
<string name="app_name">Receive Share Demo</string>
|
||||||
|
<string name="receive_share_msg">Received a share from another application. This is
|
||||||
|
the data received:</string>
|
||||||
|
<string name="send_to_service">Send to service</string>
|
||||||
|
<string name="preparing_to_process_share">Preparing to process share...</string>
|
||||||
|
</resources>
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2015, The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.example.android.receiveshare;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.ClipData;
|
||||||
|
import android.content.ContentResolver;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.res.AssetFileDescriptor;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.text.SpannableStringBuilder;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ReceiveShare extends Activity {
|
||||||
|
static Uri getShareUri(Intent intent) {
|
||||||
|
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
||||||
|
if (uri == null) {
|
||||||
|
ClipData clip = intent.getClipData();
|
||||||
|
if (clip != null && clip.getItemCount() > 0) {
|
||||||
|
uri = clip.getItemAt(0).getUri();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
static CharSequence buildShareInfo(ContentResolver resolver, Intent intent) {
|
||||||
|
SpannableStringBuilder sb = new SpannableStringBuilder();
|
||||||
|
if (intent.getType() != null) {
|
||||||
|
sb.append("Type: "); sb.append(intent.getType()); sb.append("\n");
|
||||||
|
}
|
||||||
|
CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
|
||||||
|
if (text != null) {
|
||||||
|
sb.append("Text: "); sb.append(text);
|
||||||
|
String html = intent.getStringExtra(Intent.EXTRA_HTML_TEXT);
|
||||||
|
if (html != null) {
|
||||||
|
sb.append("\n\n"); sb.append("HTML: "); sb.append(html);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Uri uri = getShareUri(intent);
|
||||||
|
if (uri != null) {
|
||||||
|
sb.append("Uri: "); sb.append(uri.toString()); sb.append("\n");
|
||||||
|
try {
|
||||||
|
AssetFileDescriptor afd = resolver.openAssetFileDescriptor(
|
||||||
|
uri, "r");
|
||||||
|
sb.append("Start offset: ");
|
||||||
|
sb.append(Long.toString(afd.getStartOffset()));
|
||||||
|
sb.append("\n");
|
||||||
|
sb.append("Length: ");
|
||||||
|
sb.append(Long.toString(afd.getLength()));
|
||||||
|
sb.append("\n");
|
||||||
|
afd.close();
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
sb.append(e.toString());
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
sb.append(e.toString());
|
||||||
|
} catch (IOException e) {
|
||||||
|
sb.append(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
setContentView(R.layout.receive_share);
|
||||||
|
|
||||||
|
Button sendButton = (Button)findViewById(R.id.send_to_service);
|
||||||
|
final Uri uri = getShareUri(getIntent());
|
||||||
|
if (uri != null) {
|
||||||
|
sendButton.setEnabled(true);
|
||||||
|
} else {
|
||||||
|
sendButton.setEnabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
TextView content = (TextView)findViewById(R.id.receive_share_data);
|
||||||
|
content.append(buildShareInfo(getContentResolver(), getIntent()));
|
||||||
|
|
||||||
|
// Watch for button clicks.
|
||||||
|
sendButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
Intent intent = new Intent(ReceiveShare.this, ReceiveShareService.class);
|
||||||
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||||
|
ClipData clip = ClipData.newUri(getContentResolver(), "Something", uri);
|
||||||
|
intent.setClipData(clip);
|
||||||
|
startService(intent);
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2015, The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.example.android.receiveshare;
|
||||||
|
|
||||||
|
import android.app.IntentService;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
public class ReceiveShareService extends IntentService {
|
||||||
|
Handler mHandler;
|
||||||
|
|
||||||
|
public ReceiveShareService() {
|
||||||
|
super("ReceiveShareService");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onCreate() {
|
||||||
|
super.onCreate();
|
||||||
|
mHandler = new Handler(getMainLooper());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onHandleIntent(Intent intent) {
|
||||||
|
mHandler.post(new Runnable() {
|
||||||
|
@Override public void run() {
|
||||||
|
Toast.makeText(ReceiveShareService.this, R.string.preparing_to_process_share,
|
||||||
|
Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
// Give the activity a chance to finish.
|
||||||
|
Thread.sleep(5*1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
final CharSequence text = ReceiveShare.buildShareInfo(getContentResolver(), intent);
|
||||||
|
mHandler.post(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Toast.makeText(ReceiveShareService.this, text, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Log.i("ReceiveShare", text.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user