简体   繁体   中英

How do I pass values from a Single View (Layout) into Multiple java classes?

I'm building a notification service in my app and for that, I have created a layout that allows the user to input a date(via date picker) and time(via time picker) and the values get displayed in TextViews. I have two classes, one is the main(which implements the layout)

the other one is the BroadcastManager class,

public class BroadcastManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    try {
        String yourDate //must define
        String yourHour //these two
        Date d = new Date();
        DateFormat date = new SimpleDateFormat("dd/MM/yyyy");
        DateFormat hour = new SimpleDateFormat("HH:mm:ss");
        if (date.equals(yourDate) && hour.equals(yourHour)){
            Intent it =  new Intent(context, MainActivity.class);
            createNotification(context, it, "Time to refresh", "Take a Deep Breath", "It's time for your daily meditation");
        }
    }catch (Exception e){
        Log.i("date","error == "+e.getMessage());
    }
}


public void createNotification(Context context, Intent intent, CharSequence ticker, CharSequence title, CharSequence descricao){
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent p = PendingIntent.getActivity(context, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setTicker(ticker);
    builder.setContentTitle(title);
    builder.setContentText(descricao);
    builder.setSmallIcon(R.drawable.meditatebutton);
    builder.setContentIntent(p);
    Notification n = builder.build();
    //create the notification
    n.vibrate = new long[]{150, 300, 150, 400};
    n.flags = Notification.FLAG_AUTO_CANCEL;
    nm.notify(R.drawable.meditatebutton, n);
    //create a vibration
    try{

        Uri som = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone toque = RingtoneManager.getRingtone(context, som);
        toque.play();
    }
    catch(Exception e){}
}}

as you can see I must set your-date and your-hour variables whose values are in the TextViews that can be accessed by the main class only, How do I share these values among classes and solve this problem?

Ps- I'm implementing these classes according to this Set notification for specific date and time

try This

Intent intent = new Intent("com.example.Broadcast");
intent.putExtra("date", datetime); 
sendBroadcast(intent);

In MyClass

public void onReceive(Context context, Intent intent) {
 String action = intent.getAction();

 if(action.equals("com.example.Broadcast")){
  String dateTime = intent.getExtras().getString("date");
 }
}

IN your android manifest file

<receiver android:name="MyReceiver" >
<intent-filter>
    <action android:name="com.example.Broadcast" >
    </action>
</intent-filter>

for more info check this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM