我创建了使用Service class在特定日期发送电子邮件的应用程序。我已尝试并运行其获取runTimeException。如何在特定日期使用服务类发送电子邮件。谢谢欣赏。
这是我的活动代码
btnSubmit = (Button) findViewById(R.id.buttonSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
strEmail= etEmailId.getText().toString().trim();
strServiceName = etServiceName.getText().toString().trim();
strSuppliername= etSupplierName.getText().toString().trim();
strReplacementDate= etReplacementDate.getText().toString().trim();
strIntervalDays= etInterval_Days.getText().toString().trim();
strNextReplacementDate = etNextReplanishmentDate.getText().toString().trim();
System.out.println("Data :- " + strServiceName +","+ strSuppliername +","+ strEmail +","+ strReplacementDate +","+ strIntervalDays + "," + strNextReplacementDate);
SharedPreferences sharedPref =getSharedPreferences("MyPref_Email", 0);
SharedPreferences.Editor editor1 = sharedPref.edit();
editor1.putString("ServiceName", strServiceName);
editor1.putString("Suppliername", strSuppliername);
editor1.putString("Email", strEmail);
editor1.putString("ReplacementDate", strReplacementDate);
editor1.putString("IntervalDays", strIntervalDays);
editor1.putString("NextReplacementDate", strNextReplacementDate);
editor1.commit();
Log.e("Commit is Complit", "Save");
// sendEmail();
System.out.println("Email Has To Be Sent Successfully");
databaseHelper.insertServicenDetails(strServiceName, strSuppliername, strEmail, strReplacementDate, strIntervalDays, strNextReplacementDate);
System.out.println("Data Has Been Save Successfully");
Intent intent = new Intent(Xyz.this, EmailService.class);
startService(intent);
System.out.println("Service is start !!!!");
}
});
Here is my Email Service class
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();
}
}
}
Here is my Log Cat error
08-16 12:53:17.112: E/AndroidRuntime(550): FATAL EXCEPTION: main
08-16 12:53:17.112: E/AndroidRuntime(550): java.lang.RuntimeException: Unable to start service com.demo.EmailService@4050d8b8 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 12:53:17.112: E/AndroidRuntime(550): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2052)
08-16 12:53:17.112: E/AndroidRuntime(550): at android.app.ActivityThread.access$2800(ActivityThread.java:117)
08-16 12:53:17.112: E/AndroidRuntime(550): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:994)
08-16 12:53:17.112: E/AndroidRuntime(550): at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 12:53:17.112: E/AndroidRuntime(550): at android.os.Looper.loop(Looper.java:123)
08-16 12:53:17.112: E/AndroidRuntime(550): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-16 12:53:17.112: E/AndroidRuntime(550): at java.lang.reflect.Method.invokeNative(Native Method)
08-16 12:53:17.112: E/AndroidRuntime(550): at java.lang.reflect.Method.invoke(Method.java:507)
08-16 12:53:17.112: E/AndroidRuntime(550): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-16 12:53:17.112: E/AndroidRuntime(550): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-16 12:53:17.112: E/AndroidRuntime(550): at dalvik.system.NativeStart.main(Native Method)
08-16 12:53:17.112: E/AndroidRuntime(550): 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 12:53:17.112: E/AndroidRuntime(550): at android.app.ContextImpl.startActivity(ContextImpl.java:621)
08-16 12:53:17.112: E/AndroidRuntime(550): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
08-16 12:53:17.112: E/AndroidRuntime(550): at com.demo.EmailService.sendEmail(EmailService.java:86)
08-16 12:53:17.112: E/AndroidRuntime(550): at com.demo.EmailService.onStartCommand(EmailService.java:44)
08-16 12:53:17.112: E/AndroidRuntime(550): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2039)
08-16 12:53:17.112: E/AndroidRuntime(550): ... 10 more