如何使通知全天随机出现?

伊利亚·斯特德

您好,我试图在一天中的随机时间发出通知。现在,它所做的就是使通知在我按下a按钮时出现。我希望按钮开始使它们每隔一个小时左右出现一次。这是我的MainActivity类。

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

}

尼尔斯

在这里查看:https : //developer.android.com/training/scheduling/alarms.html

您需要做的是发出警报,例如,它会根据需要每小时启动一次您的应用程序代码。在此警报中,您将发出新通知。

如果您需要代码示例,请查看此处:警报管理器示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何随机发送通知?

来自分类Dev

如何计算随机实例的出现

来自分类Dev

使用Python全天分发随机数据记录

来自分类Dev

如何解决随机数的出现

来自分类Dev

FullCalendar列表视图-如何隐藏“全天”

来自分类Dev

如何全天自动创建Elasticsearch索引模式?

来自分类Dev

rfc2445,如何存储全天活动?

来自分类Dev

如何在 fullcalendar 中创建全天活动

来自分类Dev

出现UIViewController时如何获得通知?

来自分类Dev

如何在随机时间每天随机安排一次本地通知

来自分类Dev

WordPress-如何使随机帖子出现在首页上?

来自分类Dev

如何使随机输入出现在jQuery中?

来自分类Dev

如何使随机输入出现在jQuery中?

来自分类Dev

MySQL-如何显示特定月份的全天记录?

来自分类Dev

如何让Python脚本全天候运行24/7

来自分类Dev

如何使用Google Calendar API创建“全天”活动

来自分类Dev

如何从Google日历api搜索中排除全天活动?

来自分类Dev

如何获得全天使用的Internet数据量?

来自分类Dev

如何从云中 24/7 全天候运行代码

来自分类Dev

如何显示累积的推送通知,而不是显示往往同时出现的多个通知?

来自分类Dev

如何显示累积的推送通知,而不是显示往往同时出现的多个通知?

来自分类Dev

如何使数字在某个时间百分比内随机出现-重复出现?

来自分类Dev

休眠错误随机出现

来自分类Dev

方糖使随机图像出现

来自分类Dev

通知不会出现

来自分类Dev

如何从可绘制文件夹中插入随机图像,使其在加载时随机出现在屏幕上?

来自分类Dev

计算随机数的出现

来自分类Dev

NAS出现随机IO错误

来自分类Dev

我随机出现jquery错误

Related 相关文章

热门标签

归档