gpt4 book ai didi

java - 使用 Android 服务发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 12:38:38 29 4
gpt4 key购买 nike

我想在特定日期发送电子邮件。我正在开发一项服务,将在特定日期发送电子邮件。我花了几个小时试图弄清楚如何在服务后台的特定日期发送电子邮件,即使我的键盘被锁定。有人可以帮我怎么做吗?

这是日志猫信息

08-16 15:18:00.122: E/AndroidRuntime(632): java.lang.RuntimeException: Unable to start service com.demo.EmailService@40561c20 with Intent { cmp=com.demo/.EmailService }: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
08-16 15:18:00.122: E/AndroidRuntime(632): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2052)
08-16 15:18:00.122: E/AndroidRuntime(632): at android.app.ActivityThread.access$2800(ActivityThread.java:117)
08-16 15:18:00.122: E/AndroidRuntime(632): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:994)
08-16 15:18:00.122: E/AndroidRuntime(632): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 15:18:00.122: E/AndroidRuntime(632): at android.os.Looper.loop(Looper.java:123)
08-16 15:18:00.122: E/AndroidRuntime(632): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 15:18:00.122: E/AndroidRuntime(632): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 15:18:00.122: E/AndroidRuntime(632): at java.lang.reflect.Method.invoke(Method.java:507)
08-16 15:18:00.122: E/AndroidRuntime(632): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 15:18:00.122: E/AndroidRuntime(632): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 15:18:00.122: E/AndroidRuntime(632): at dalvik.system.NativeStart.main(Native Method)
08-16 15:18:00.122: E/AndroidRuntime(632): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
08-16 15:18:00.122: E/AndroidRuntime(632): at android.app.ContextImpl.startActivity(ContextImpl.java:621)
08-16 15:18:00.122: E/AndroidRuntime(632): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
08-16 15:18:00.122: E/AndroidRuntime(632): at com.demo.EmailService.sendEmail(EmailService.java:86)
08-16 15:18:00.122: E/AndroidRuntime(632): at com.demo.EmailService.onStartCommand(EmailService.java:44)
08-16 15:18:00.122: E/AndroidRuntime(632): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2039)

这是我的服务代码

public class EmailService extends Service 
{
private static final String TAG = null;
private static final String TAG1="MyService";
private static final String LOGTAG = null;


String strServiceName ;
String strSuppliername ;
String strEmail;
String strReplacementDate ;
String strIntervalDays;
String strNextReplacementDate;

String strMyServicePref;

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

public void onCreate()
{
Toast.makeText(this,"My Service Created",Toast.LENGTH_LONG).show();
Log.d(TAG,"ON CREATE");
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
startService(intent);
sendEmail();
return Service.START_STICKY;
}


protected void sendEmail()
{
SharedPreferences sharedPref1 =getSharedPreferences("MyPref_CellNumber", 0);
SharedPreferences.Editor editor1 = sharedPref1.edit();

strServiceName=sharedPref1.getString("ServiceName" , "a\n");
strSuppliername=sharedPref1.getString("Suppliername" , "a\n");
strEmail=sharedPref1.getString("Email" , "a\n");
strReplacementDate=sharedPref1.getString("ReplacementDate" , "a\n");
strIntervalDays=sharedPref1.getString("IntervalDays" , "a\n");
strNextReplacementDate=sharedPref1.getString("NextReplacementDate" , "a\n");

Log.e(TAG,"ON CREATE" + strServiceName);
Log.e(TAG,"ON CREATE" + strSuppliername);
Log.e(TAG,"ON CREATE" + strEmail);
Log.e(TAG,"ON CREATE" + strReplacementDate);
Log.e(TAG,"ON CREATE" + strIntervalDays);
Log.e(TAG,"ON CREATE" + strNextReplacementDate);

String subject = strServiceName;

String body = strServiceName+","
+strSuppliername+","
+strReplacementDate+","
+strIntervalDays+","
+strNextReplacementDate;

Log.e("body " , " = " + body);

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{strEmail});
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT , body);

try
{
startActivity(Intent.createChooser(i, "Send mail...in Service Background"));
}
catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(EmailService.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
}

最佳答案

我认为你应该使用警报管理器。

The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.

关于java - 使用 Android 服务发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25338964/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com