如何使用非活动类和片段中的隐式意图

时间:2015-01-20 06:49:55

标签: android android-intent android-activity android-fragments

public class HomeFragment extends Fragment {
ShareLink statLoc;

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {   
    super.onViewCreated(view, savedInstanceState);

statLoc = new ShareLink();
statLoc.execute();
}

上面的代码是我的片段,正在执行一个名为ShareLink()的类。 在该ShareLink类中,我使用隐式intent来发送如下所示的数据

public class ShareLink extends AsyncTask<String, String, String> {

String urlShare = DeviceIdGen.hmUrl;

String statusLoc = null;
public static String locLink = null;
private static String loc = null;   
Context context;    
String locUrl;

protected String doInBackground(String... params) {     

    WebServiceTasks wstShare = new WebServiceTasks();
    return wstShare.getMethod(urlShare);
}

protected void onPostExecute(String result) {   
    super.onPostExecute(result);    

    try {
        JSONObject jResult = new JSONObject(result);
        statusLoc = jResult.getString("status");

        if (statusLoc.equals("success")) {
            loc = jResult.getString("link");                
        locUrl = "My Current Location :" + locLink + "\n\n"
                    + "My Key : " + loc;            

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("text/plain");
        startActivity((emailIntent).putExtra(
        android.content.Intent.EXTRA_TEXT, locUrl));
        } 
    } catch (JSONException e) {

        e.printStackTrace();
    }}}

但我无法实现它。请帮助我新手

2 个答案:

答案 0 :(得分:2)

只需编辑您的AsyncTask, 添加构造函数

public class ShareLink extends AsyncTask<String, String, String> {

String urlShare = DeviceIdGen.hmUrl;

String statusLoc = null;
public static String locLink = null;
private static String loc = null;   

String locUrl;

private Context context = null;
    public ShareLink(Context context) {
        this.context = context;
    }
protected String doInBackground(String... params) {     

    WebServiceTasks wstShare = new WebServiceTasks();
    return wstShare.getMethod(urlShare);
}

protected void onPostExecute(String result) {   
    super.onPostExecute(result);    

    try {
        JSONObject jResult = new JSONObject(result);
        statusLoc = jResult.getString("status");

        if (statusLoc.equals("success")) {
            loc = jResult.getString("link");                
        locUrl = "My Current Location :" + locLink + "\n\n"
                    + "My Key : " + loc;            

        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("text/plain");
        context.startActivity((emailIntent).putExtra(
        android.content.Intent.EXTRA_TEXT, locUrl));
        } 
    } catch (JSONException e) {

        e.printStackTrace();
    }}}

并调用您的AsyncTask

statLoc = new ShareLink(getActivity());
statLoc.execute();

答案 1 :(得分:1)

您可以将上下文或活动传递给ShareLink类,并使用该实例启动您的意图。 例如

private Context context;
public ShareLink (Context context){
    this.context = context;
}

然后在您的onPostExecute中,您可以使用context.startActivity()或任何您想要的意图。