简体   繁体   中英

How do i make a notification appear at random times throughout the day?

Hi im trying to make a notification appear at random times throughout the day. right now all it does is make the notification appear when i press the a button. I would like the button to start making them appear every hour or so. Here is my MainActivity class.

public class MainActivity extends Activity {
Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NotificationCompat.Builder notification = new NotificationCompat.Builder(MainActivity.this);

            notification.setSmallIcon(R.mipmap.ic_launcher);
            notification.setTicker("Hey!!!");
            notification.setWhen(System.currentTimeMillis());
            notification.setContentTitle("You're Awesome");

            Uri sound = RingtoneManager.getDefaultUri(Notification.DEFAULT_SOUND);

            notification.setContentText("keep being awesome!!!:)");
            notification.setSound(sound);

            Bitmap picture = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

            notification.setLargeIcon(picture);

            PendingIntent mypendingintent;
            Intent myintent = new Intent();
            Context mycontext = getApplicationContext();
            myintent.setClass(mycontext, Activity2.class);
            myintent.putExtra("ID", 1);
            mypendingintent = PendingIntent.getActivity(mycontext, 0, myintent, 0);

            notification.setContentIntent(mypendingintent);
            Notification n = notification.build();

            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            nm.notify(1,n);




        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Look over here: https://developer.android.com/training/scheduling/alarms.html

What you need to do is make an alarm, for example which will activate your app code every hour like you want. In this alarm you will make a new notification.

If you need a code example look over here: Alarm Manager Example

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