将EditText保存到数组中(不同的活动)

时间:2017-03-16 12:14:03

标签: java android arrays android-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("notification@gmail.com");
    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));
}
}

4 个答案:

答案 0 :(得分:2)

您可以使用SharedPreference在另一个活动中获取字符串值: 在EditText活动中:

全球宣布:

SharedPreferences pref;

在两个活动的onCreate()中启动:

pref = PreferenceManager.getDefaultSharedPreferences(this); //Here is my change

editText中初始化onCreate()

subjectEditText = (EditText) findViewById(R.id.subject_edittext);
descEditText = (EditText) findViewById(R.id.description_edittext);
emailEditText = (EditText) findViewById(R.id.email_edittext);

btnCreate()方法中:

String email = emailEditText.getText().toString();
pref.edit().putString("email", email).apply();

在你的String []活动中:

String getEmail = pref.getString("email", "");
String[] toArr = {getEmail};

希望这有帮助。

答案 1 :(得分:1)

将其写入共享首选项和其他活动检查,如果有电子邮件,则从共享首选项加载它。 http://androidopentutorials.com/android-sharedpreferences-tutorial-and-example/ 这是理解共享偏好的好的简单示例。 如果你想在运行时运行它,你也可以通过Intents传递它。研究意图和共享偏好。

答案 2 :(得分:1)

如果要在运行时存储这些电子邮件地址字符串,以后不再需要它们,请在MainActivity中定义public static String [],然后从不同的活动中添加值。 在MainActivity(或第一个Activity(我的推荐))中定义它

public static String[] emails;

在另一个活动中调用它并添加到其元素但是为了避免覆盖执行此操作

int count = MainActivity.emails.length;
MainActivity.emails = new String[count + 1];
MainActivity.emails[count + 1] = "newemail@mail.com";

但是如果你想保存它并在以后更好地保存在文件中。 希望它有所帮助!

答案 3 :(得分:1)

像这样

String[] all_emails= new String[10]; // initialise array

String email= edutText.getText().toString(); // Get text from EditText
all_emails[index] = email;  //add to array like... all_emails[0] = email;