当我调用PublicParFun.ShareAPP(getApplicationContext());我添加了FLAG_ACTIVITY_NEW_TASK标志时,仍然收到错误消息“从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志”。

顺便说一句,PublicParFun.ShowMainUI(getApplicationContext())可以很好地工作,为什么?

CallerMin.java

package ui;

import info.dodata.callernametalker.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import bll.PublicParFun;

public class CallerMain extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.caller_main);
        SetButtons();
    }

    private void SetButtons() {
        findViewById(R.id.btnSettings).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PublicParFun.ShowMainUI(getApplicationContext());
            }
        });

        findViewById(R.id.btnShare).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PublicParFun.ShareAPP(getApplicationContext());
            }
        });

        findViewById(R.id.btnClose).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}


PublicParFun.java

package bll;

public class PublicParFun {

    public static void ShowMainUI(Context myContext){
        Intent intent = new Intent().setClass(myContext,ui.CallerMain.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        myContext.startActivity(intent);
    }


    public static void ShareAPP(Context myContext){
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

        intent.putExtra(Intent.EXTRA_TITLE, myContext.getString(R.string.ShareTitle));
        intent.putExtra(Intent.EXTRA_SUBJECT, myContext.getString(R.string.ShareTitle));
        intent.putExtra(Intent.EXTRA_TEXT, myContext.getString(R.string.ShareContent));

        try {
            myContext.startActivity(Intent.createChooser(intent,
                    myContext.getString(R.string.defautlClient)));

        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(myContext,
                    myContext.getString(R.string.defautlNoClient), Toast.LENGTH_SHORT)
                    .show();
        }
    }


}

最佳答案

在CallerMain活动类中

而不是里面的getApplicationContext()

PublicParFun.ShowMainUI(getApplicationContext());


做CallerMain.this为

PublicParFun.ShowMainUI(CallerMain.this);

10-08 07:43