gpt4 book ai didi

java - 如何将值从单个 View (布局)传递到多个 java 类?

转载 作者:行者123 更新时间:2023-12-02 00:58:23 24 4
gpt4 key购买 nike

我正在我的应用程序中构建通知服务,为此,我创建了一个布局,允许用户输入日期(通过日期选择器)和时间(通过时间选择器),并且值显示在 TextView 中。我有两个类,一个是主类(实现布局)

另一个是BroadcastManager类,

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){}
}}

如您所见,我必须设置“您的日期”和“您的时间”变量,其值位于只能由主类访问的 TextView 中,如何在类之间共享这些值并解决此问题?

Ps-我正在根据此 Set notification for specific date and time 实现这些类

最佳答案

试试这个

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

在我的类(class)

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

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

在你的 Android list 文件中

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

查看更多信息 this link

关于java - 如何将值从单个 View (布局)传递到多个 java 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57789756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com