notify every day schedule X

Asked

Viewed 21 times

0

I was running the code in android studio but the code has some flaws and I can not solve,.

Mainactivity

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){
        @Override
        public void  onClick(View view){

            Calendar calendar = Calendar.getInstance();

            Calendar.set(Calendar.HOUR_OF_DAY,18);
            Calendar.set(Calendar.MINUTE,45);
            Calendar.set(Calendar.SECOND,00);


            Intent intent = new Intent(getApplicationContext(),Notification_reciever.class);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),100,intent,PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
        }
    });
}

}

Notification_reciever

class Notification_reciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent repeating_intent = new Intent(context,Reprating_activity.class);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(android.R.drawable.arrow_up_float)
            .setContentText("titulo")
            .setContentText("texto")
            .setAutoCancel(true);
    notificationManager.notify(188,builder.build());

}

}

Reprating_activity

class Reprating_activity extends AppCompatActivity{
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.repeating_activity_layout);

}

}

manifest

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

<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=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".Reprating_activity"/>

    <receiver android:name=".Notification_reciever"/>
</application>
  • Duplicate of where I have doubt what is wrong in my code.

  • "... but the code has some flaws ...". What "flaws" refers to?

  • The flaws that appears is getInstance there in Calendar Calendar Calendar = Calendar.getInstance(); in Mainactivity fault in . set in the 3 line Calendar.set(Calendar.); and getTimeInMillis failed in the last line of Mainactivity

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.