如何使用非 activity class 和片段中的隐式意图

How to use implicit intent from non activity class and in fragment

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() 的 class。 在那个 ShareLink class 中,我使用隐式意图发送如下数据

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();
    }}}

但我无法实现。作为新手请帮忙

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

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

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

只需编辑您的 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();