应用“通过分享'运行没有查看 [英] 'Share via' application that runs without a View
问题描述
我想要写在菜单中的分享方式坐(快速传送电子邮件给自己链接到的东西,我觉得在网络上还是在RSS阅读器看)对于这个,我宣布我的应用程序意图的应用程序。 action.SEND意图过滤器:
<活动
机器人:名字=uk.co.baroquedub.checkit.MainActivity
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.SEND/>
<类机器人:名字=android.intent.category.DEFAULT/>
<数据机器人:mime类型=text / plain的/>
&所述; /意图滤光器>
< /活性GT;
下面是在MainActivity包的骨架
包uk.co.baroquedub.testcheck;进口android.app.Activity;
进口android.os.AsyncTask;
进口android.os.Bundle;进口android.widget.Toast;公共类MainActivity延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);//真正的code这里抓起URL从意向的电子邮件,然后将其作为AsyncTask的:
doSendTask任务=新doSendTask();
task.execute(新的String [] {urlString});
}保护无效的ShowDialog(字符串响应){
Toast.makeText(这一点,对此,Toast.LENGTH_SHORT).show();
完();
}私有类doSendTask扩展的AsyncTask<弦乐,太虚,字符串> {
@覆盖
保护字符串doInBackground(字符串的URL ...){
串响应=;
//真code这里发出的电子邮件
//模拟等待电子邮件发送:
尝试{
视频下载(5000);
响应=娇娇
}
赶上(InterruptedException的前){} 返回响应;
} @覆盖
保护无效onPostExecute(字符串结果){
的ShowDialog(结果);
}
}}
问题是,我的应用程序在浏览器的顶部打开(会出现一个白色的屏幕显示应用程序的名称的标题栏) - 这是从正在直到等待访问停止浏览器完成(因此击败我的包裹在AsyncTask的内sendEmail功能)的目的。
请参阅:对问题的演示截屏
请参阅:<一href=\"http://stackoverflow.com/questions/13704146/app-using-asynctask-still-hogging-the-ui-thread\">related全code 问题
谁能告诉我,我怎么能有我的应用程序启动(从菜单中选择通过股份),并执行我的code,但实际上不具有查看(如果这是在空白屏幕和所有权权利的术语酒吧)?
- 开始的活动没有任何UI
- 启动服务做你的后台服务的OnCreate
- 只要你启动服务结束活动
- 让后期服务的通知或吐司约完成。
如果你想显示一个对话框,你可以用刚刚从服务对话框启动一个单独的活动,但它通常是侵入显示对话框。
I want to write an app that sits in the 'Share via' menu (for quickly emailing myself links to things I find on the web or look at in RSS readers) For this I'm declaring my app with an intent.action.SEND intent-filter:
<activity
android:name="uk.co.baroquedub.checkit.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Here's a skeleton of the MainActivity package
package uk.co.baroquedub.testcheck;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// real code here grabs URL from intent then emails it as an asyncTask:
doSendTask task = new doSendTask();
task.execute(new String[] { "urlString" });
}
protected void showDialog (String response){
Toast.makeText(this, response, Toast.LENGTH_SHORT).show();
finish();
}
private class doSendTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
// Real code here sends the email
// Simulate waiting for the email to be sent:
try {
Thread.sleep(5000);
response = "Waited";
}
catch (InterruptedException ex) { }
return response;
}
@Override
protected void onPostExecute(String result) {
showDialog(result);
}
}
}
The problem is that my app is opening on top of the browser (a white screen appears with a title bar showing the name of the app) - which is stopping the browser from being accessible until the 'wait' is finished (hence defeating the purpose of wrapping my sendEmail functionality within an asyncTask).
See: screencast for demo of problem
See: related question with full code
Can anyone tell me how I can have my app start (from the 'Share via' menu) and execute my code but without actually having a 'View' (if that's the right terminology for the blank screen and title bar)?
- start an activity without any UI
- start a service to do your background service in OnCreate
- finish the activity as soon as you start service
- let the service post notification or Toast about completion.
If you do want to show a dialog, you could start a separate activity with just the dialog from service, but it is usually intrusive to show dialog.
这篇关于应用“通过分享'运行没有查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!