日期选择器时间选择器无法在 Android Studio 中工作

Date Picker Time Picker Cant work in Android Studio

在那里我想在特定的日期和时间用编辑文本变量发送通知。我在我的项目中使用 DatePicker、TimePicker、AlertReciever.class、edittexts。它无法推送通知。我试过了,但没用。我如何解决我的问题 它如何工作。你能不能快点对我来说很重要。为什么它不能工作。

activty.class

public class hatirlatici extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener,DatePickerDialog.OnDateSetListener {
    Calendar now = Calendar.getInstance();
    Button hatirlaticitarihsaat,backhome;
    EditText editisim,editaciklama;
    ImageView tick;
    TextView textbasariylakuruldu,texthatirlaticibas,hatirlaticiaciktext,zaman;
    private int notificationId = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hatirlatici);
        zaman=findViewById(R.id.zaman);
        editisim=findViewById(R.id.hatbaslik);
        tick=findViewById(R.id.tick);
        textbasariylakuruldu=findViewById(R.id.textbasariylakuruldu);
        texthatirlaticibas=findViewById(R.id.texthatirlaticibas);
        hatirlaticiaciktext=findViewById(R.id.hatirlaticiaciktext);
        editaciklama=findViewById(R.id.hataciklama);
        hatirlaticitarihsaat= findViewById(R.id.taarihsaatbutton);
        hatirlaticitarihsaat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment datePicker = new DatePickerFragment();
                datePicker.show(getSupportFragmentManager(), "date picker");
            }
        });


    }
    public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
        now.set(Calendar.HOUR_OF_DAY, hourOfDay);
        now.set(Calendar.MINUTE, minute);
        now.set(Calendar.SECOND, 0);

        Intent intent = new Intent(hatirlatici.this, AlertReciever.class);
        intent.putExtra("notificationId", notificationId);
        intent.putExtra("messagex", editaciklama.getText().toString());
        intent.putExtra("titlex", editisim.getText().toString());
        PendingIntent alarmintent = PendingIntent.getBroadcast(
                hatirlatici.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT
        );
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar startalarm = Calendar.getInstance();
        startalarm.set(Calendar.HOUR_OF_DAY, hourOfDay);
        startalarm.set(Calendar.SECOND, 0);
        startalarm.set(Calendar.MINUTE, minute);
        long alarmstartTime = startalarm.getTimeInMillis();
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmstartTime, alarmintent);
    }
    private void startNotification(Calendar now){
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this,AlertReciever.class);
        intent.putExtra("Isım_ID", editisim.getText().toString());
        intent.putExtra("Aciklama", editaciklama.getText().toString());
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,1,intent,0);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pendingIntent);

    }


    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        now.set(Calendar.YEAR, year);
        now.set(Calendar.MONTH, month);
        now.set(Calendar.DAY_OF_MONTH, day);
        DialogFragment timePicker = new TimePickerFragment();
        timePicker.show(getSupportFragmentManager(), "time picker");
        String Datex = DateFormat.getDateInstance(DateFormat.DATE_FIELD).format(now.getTime());
        zaman.setText(Datex);
        Calendar startalarm = Calendar.getInstance();

        startalarm.set(Calendar.DAY_OF_MONTH,day );
        startalarm.set(Calendar.YEAR, year);
        startalarm.set(Calendar.MONTH, month);
        Intent intent = new Intent(hatirlatici.this, AlertReciever.class);
        PendingIntent alarmintent = PendingIntent.getBroadcast(
                hatirlatici.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT
        );
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        long alarmstartTime = startalarm.getTimeInMillis();
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmstartTime, alarmintent);


    }
}

AlertReciever.class

public class AlertReciever extends BroadcastReceiver {
    private Notification notification;
    private static final String ChannelID = "Sample_Channel";

    @Override
    public void onReceive(Context context, Intent intent) {
        int NotificationId = intent.getIntExtra("notificationId", 0);
        String message = intent.getStringExtra("messagex");
        String title = intent.getStringExtra("titlex");
        Intent mainIntent = new Intent(context, hatirlatici.class);
        PendingIntent contentIntent = PendingIntent.getActivity(
                context,0,mainIntent,0
        );
  NotificationManager notificationManager =
          (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
      CharSequence channelName = "My Notification";
      int importance = NotificationManager.IMPORTANCE_DEFAULT;
      NotificationChannel channel = new NotificationChannel(ChannelID,channelName,importance);
      notificationManager.createNotificationChannel(channel);

        }

  NotificationCompat.Builder builder = new NotificationCompat.Builder(context, ChannelID)
          .setContentText(message)
          .setContentTitle(title)
          .setContentIntent(contentIntent)
          .setPriority(NotificationCompat.PRIORITY_DEFAULT)
          .setSmallIcon(R.drawable.ic_baseline_menu_book_24);

    notificationManager.notify(NotificationId, builder.build());
    }


}

TimePickerFragment.class

 package com.viado;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import java.util.Calendar;

public class TimePickerFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getActivity(), hour,minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
    }
}

DatePickerFragment.class

package com.viado;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import java.util.Calendar;

public class DatePickerFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);


        return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(),year ,month,day);


    }
}

更新

activty.class

public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
        now.set(Calendar.HOUR_OF_DAY, hourOfDay);
        now.set(Calendar.MINUTE, minute);
        now.set(Calendar.SECOND, 0);

        Intent intent = new Intent(hatirlatici.this, AlertReciever.class);
        intent.setAction("ALERT_RECIEVER");
        intent.putExtra("notificationId", notificationId);
        intent.putExtra("messagex", editaciklama.getText().toString());
        intent.putExtra("titlex", editisim.getText().toString());

        PendingIntent alarmintent = PendingIntent.getBroadcast(
                hatirlatici.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT
        );
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar startalarm = Calendar.getInstance();
        startalarm.set(Calendar.HOUR_OF_DAY, hourOfDay);
        startalarm.set(Calendar.SECOND, 0);
        startalarm.set(Calendar.MINUTE, minute);
        long alarmstartTime = startalarm.getTimeInMillis();
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmstartTime, alarmintent);

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.viado">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".oncekipomodoro"></activity>
        <activity android:name=".settings" />
        <activity android:name=".feedback" />
        <activity android:name=".todolist" />
        <activity android:name=".hatirlatici" />
        <activity android:name=".pomodoro" />
        <activity android:name=".premium" />

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-1122513733180723~9899208652" />

        <activity android:name=".pomodoroscreen" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".AlertReciever">
            <intent-filter>
                <action android:name="ALERT_RECIEVER" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

您的代码看起来不错,我认为可能的问题是您没有在清单文件中声明 AlertReciever,因此请确保您在 application 清单文件中的标记。这是因为 BroadcastReciever 被认为是必须在清单中声明的​​系统组件,否则不会调用 onReceive() 方法。

<application 
    ......
    <receiver android:name=".AlertReciever">
        <intent-filter>
            <action android:name="ALERT_RECIEVER" />
        </intent-filter>
    </receiver>

此外,您需要将上面提到的相同操作设置为您创建的意图以触发此 AlertReceiver,因此在您的 onTimeSet() 方法中添加:

intent.setAction("ALERT_RECIEVER");