有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

感谢您抽出时间阅读我的帖子

我如何才能从Create中保存EditText(emailEditText)中电子邮件的价值。java在activity AlarmService中加入这个数组

上面写着String[] toArr =

有没有人能提供解决方案或提出建议?我试过多次都没有成功

任何帮助都将不胜感激!:)

报警服务。java

package com.example.pc.achieve;

import 安卓.app.IntentService;
import 安卓.app.NotificationManager;
import 安卓.app.PendingIntent;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.content.SharedPreferences;
import 安卓.os.PowerManager;
import 安卓.preference.PreferenceManager;
import 安卓.support.v4.app.NotificationCompat;
import 安卓.util.Log;
import 安卓.widget.Toast;

import static 安卓.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();
}
}

创建。java

package com.example.pc.achieve;

import 安卓.app.Activity;
import 安卓.content.DialogInterface;
import 安卓.content.Intent;
import 安卓.content.SharedPreferences;
import 安卓.os.Bundle;
import 安卓.preference.PreferenceManager;
import 安卓.support.v7.app.AlertDialog;
import 安卓.view.View;
import 安卓.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));
}
}

共 (1) 个答案

  1. # 1 楼答案

    如果您想在运行时存储这些电子邮件地址字符串,并且以后不需要它们,那么请在MainActivity中定义公共静态字符串[],然后从不同的活动中添加值。 在MainActivity(或first Activity(我的建议))中定义它

    public static String[] emails;
    

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

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

    但如果你想保存它并在以后使用它,最好保存在文件中。 希望有帮助