Упрощенный пример:
public class AsyncTaskMy extends AsyncTask<Void, Void, Void> {
private WeakReference<Context> contextRef;
public AsyncTaskMy(Context context) {
contextRef = new WeakReference<>(context);
}
@Override
protected Void doInBackground(Void... voids) {
Context context = contextRef.get();
if (context != null) {
try {
PackageManager m = context.getPackageManager();
String s = context.getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = Environment.getExternalStorageDirectory().toString();
s += p.applicationInfo.dataDir;
sendFileWithEmail(new File(s));//or whatever you want
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
return null;
}
MainActivity.java
class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AsyncTaskMy asyncTaskMy = new AsyncTaskMy(this);
asyncTaskMy.execute();
}
}