2017-03-16 19 views
-1

感謝您花時間閱讀我的文章。將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)); 
} 
} 
+0

你想從不同的活動保存到字符串數組一樣? –

+0

是我想存儲在這個數組中的電子郵件地址的字符串,此處{「[email protected]」}。 – ZenoX

+0

把這個數組放在'Bundle'中,並將其傳遞給活動。 – Bharatesh

回答

2

您可以使用SharedPreference在另一個活動,即可找到字符串值: 在你的EditText活動:

申報全球:

SharedPreferences pref; 

發起的onCreate()兩個活動:

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

初始化editText s在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}; 

希望這有助於。

+0

感謝您的回覆,我沒有onCreate()在我的第二個活動。我已經更新了原始帖子並提供了額外信息。 – ZenoX

+0

我仍然遇到應用程序崩潰問題,並說共享首選項正在嘗試保存空數據。 – ZenoX

+0

嘗試在空對象引用 – ZenoX

1

如果您想在運行時存儲這些電子郵件地址的字符串,並且以後不需要它們,請在MainActivity中定義public static String [],然後從不同的活動中添加值。 內MainActivity(或第一活動(我的建議))定義它

public static String[] emails; 
在另一個活動

調用它並添加到其元素,但爲避免覆蓋做到這一點

int count = MainActivity.emails.length; 
MainActivity.emails = new String[count + 1]; 
MainActivity.emails[count + 1] = "[email protected]"; 

,但如果你想保存並且稍後使用它可以更好地保存在文件中。 希望它有幫助!

+0

謝謝,但我需要保存電子郵件供以後使用。我仍然有問題得到這個正常工作。 – ZenoX

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;