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)?
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.
Thanks to Nandeesh for putting me on the right path. For those wanting to know exactly how to do it, here's the full solution:
1: start an activity without any UI to do this I used the following Theme in the AndroidManifest:
which makes the initial application not just transparent but completely without a UI
2: start a service to do your background service in OnCreate Here I still had to 'grab' the URL from the Share intent and pass it as an Extra to the service:
3: finish the activity as soon as you start service - see above
Note that in the service the Extras are passed to the OnStart method (not the On Create method as might be expected) See: link
4: let the service post notification or Toast about completion. I couldn't get the Service to open a Dialog notification(as per my original app), this kept on crashing the app/service but Toast works great - and as Nandeesh suggests, it's probably less intrusive.
Here's the service package:
}
NB. remember to declare the service in the manifest (inside the application tag):