感謝您花時間閱讀我的文章。將EditText保存到數組中(不同的活動)
我將如何能夠將EditText(emailEditText)中的電子郵件的值從Create.java保存到活動AlarmService中的此數組中?
凡說String[] toArr =
是否有任何人能夠提供一個解決方案或提供任何建議?我曾多次嘗試過但沒有成功。
任何幫助將不勝感激! :)
AlarmService.java
package com.example.pc.achieve;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import static android.graphics.Color.GREEN;
public class AlarmService extends IntentService {
SharedPreferences pref;
PowerManager powerManager;
PowerManager.WakeLock wakeLock;
public AlarmService() {
super("");
}
@Override
protected void onHandleIntent(Intent intent) {
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FCFCFCFC");
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
addNotification();
sendMAIL();
}
public void addNotification() {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.icon_transperent)
.setLights(GREEN, 700, 700)
.setContentTitle("Alert!")
.setContentText("This is a reminder");
Intent notificationIntent = new Intent(getApplicationContext(), MainMenu.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
builder.setVibrate(new long[] { 0, 1000, 1000, 1000, 1000 });
manager.notify(0, builder.build());
}
public void sendMAIL(){
String getEmail = pref.getString("email", "");
String[] toArr = {getEmail};
m.setTo(toArr);
m.setFrom("[email protected]");
m.setSubject("Alert!");
m.setBody("This is a reminder.");
try {
if(m.send()) {
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Create.java
package com.example.pc.achieve;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.EditText;
public class Create extends Activity {
SharedPreferences pref;
private EditText subjectEditText;
private EditText descEditText;
EditText emailEditText;
private DBManager dbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
subjectEditText = (EditText) findViewById(R.id.subject_edittext);
descEditText = (EditText) findViewById(R.id.description_edittext);
emailEditText = (EditText) findViewById(R.id.email_edittext);
}
public void btnCreate(View view) {
String email = emailEditText.getText().toString();
pref.edit().putString("email", email).apply();
dbManager = new DBManager(this);
dbManager.open();
final String name = subjectEditText.getText().toString();
final String desc = descEditText.getText().toString();
dbManager.insert(name, desc);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your deadline has now been created.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void btnSetAlarm(View view) {
startActivity(new Intent(Create.this, Alarm.class));
}
}
你想從不同的活動保存到字符串數組一樣? –
是我想存儲在這個數組中的電子郵件地址的字符串,此處{「[email protected]」}。 – ZenoX
把這個數組放在'Bundle'中,並將其傳遞給活動。 – Bharatesh