从库开始 ACTION_SEND 意向
Start ACTION_SEND Intent from Library
我想开始与客户发送电子邮件的意图。但是 context.startActivity
在图书馆项目中不起作用。你能帮助我吗?谢谢
public class email {
private Context context;
public email(Context context) {
this.context = context;
}
public void send(String to, String subject, String body) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
try {
context.startActivity(Intent.createChooser(intent, "sending..."));
}
catch (ActivityNotFoundException e) {
}
}
好的。 Logcat 说:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test.MainActivity}: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
通常我用
getActivity().getBaseContext()
在大多数情况下,但如果您遇到困难,可以使用另一种方法。
您可以定义一个抽象基础activity,然后让您的所有活动实现它,或者至少是您需要在其子项中引用上下文的活动。
`public 抽象 class 基础 Activity 扩展 Activity
...
私有静态上下文 baseContext;
...
@Override
public void onCreate() {
super.onCreate();
BaseApplication.baseContext = getApplicationContext();
...
public static Context getMyContext() {
return BaseApplication.baseContext;
}
`
然后您可以更轻松地在其他地方调用 getMyContext(),但首先只有在 getActivity().getBaseContext() 不起作用时才使用它。
我需要关于您的代码的更多细节,例如调用它的 activity 类型等,以便给出准确的答案。
在开始 activity 之前添加 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
。每当您尝试切换到 Activity 但您的原点不是 Activity 本身时,您需要设置此标志。
我自己找到了解决办法:
当我想将 context
传递给 email
class 的构造函数时,我使用了 getApplicationContext()
,当我将其更改为 this
时,它起作用了。
我想开始与客户发送电子邮件的意图。但是 context.startActivity
在图书馆项目中不起作用。你能帮助我吗?谢谢
public class email {
private Context context;
public email(Context context) {
this.context = context;
}
public void send(String to, String subject, String body) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
try {
context.startActivity(Intent.createChooser(intent, "sending..."));
}
catch (ActivityNotFoundException e) {
}
}
好的。 Logcat 说:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test.MainActivity}: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
通常我用
getActivity().getBaseContext()
在大多数情况下,但如果您遇到困难,可以使用另一种方法。
您可以定义一个抽象基础activity,然后让您的所有活动实现它,或者至少是您需要在其子项中引用上下文的活动。
`public 抽象 class 基础 Activity 扩展 Activity ... 私有静态上下文 baseContext; ...
@Override
public void onCreate() {
super.onCreate();
BaseApplication.baseContext = getApplicationContext();
...
public static Context getMyContext() {
return BaseApplication.baseContext;
}
`
然后您可以更轻松地在其他地方调用 getMyContext(),但首先只有在 getActivity().getBaseContext() 不起作用时才使用它。
我需要关于您的代码的更多细节,例如调用它的 activity 类型等,以便给出准确的答案。
在开始 activity 之前添加 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
。每当您尝试切换到 Activity 但您的原点不是 Activity 本身时,您需要设置此标志。
我自己找到了解决办法:
当我想将 context
传递给 email
class 的构造函数时,我使用了 getApplicationContext()
,当我将其更改为 this
时,它起作用了。