dimanche 26 avril 2015

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

}




Aucun commentaire:

Enregistrer un commentaire