通知用户每日的Android应用程序

时间:2017-07-23 05:38:27

标签: java android notifications

我正在尝试制作每日提供通知消息的dailypedea应用程序。我使用SQLite并且我已经创建了一个字段日期,其中我有输入日期。当系统日期与数据库中的字符串日期匹配时,它显示相应的消息。问题但是,消息不会自动通知,即当日期更改时,通知不会自动弹出。我制作了以下程序,但效率不高。它有两个光标,当日期与系统日期匹配时,对应的光标显示当达到最后一行时,我将它重定向到第一行,然后继续进行。是否有任何其他有效方法在特定条件下通知用户

package com.example.prince.notidaily;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import java.util.Calendar;


public class MainActivity extends AppCompatActivity {
   // Date date;      ///description demo
    String message;
    Calendar c;
    int flag=0;
    int day,month,year;
    String date;
   // Notification notification;
   // PendingIntent pendingIntent;
    //Intent intent;
   // NotificationManager notificationManager;
   Notification.Builder nBuilder=null;
    NotificationCompat.Builder notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c= Calendar.getInstance();


        SQLiteDatabase notidb=openOrCreateDatabase("notidb10", Context.MODE_PRIVATE,null);
        Toast.makeText(getApplicationContext(), "db created", Toast.LENGTH_LONG).show();


        day=c.get(Calendar.DAY_OF_MONTH);
        month=c.get(Calendar.MONTH);
        ++month;
        year=c.get(Calendar.YEAR);
        date = ""+month+"/"+day+"/"+year;
        Toast.makeText(getApplicationContext(), "date is"+date, Toast.LENGTH_LONG).show();

        String create="create table if not exists notitable2(date TEXT,message TEXT)";
        notidb.execSQL(create);
        Toast.makeText(getApplicationContext(), "notitable2 table created", Toast.LENGTH_LONG).show();
        ContentValues values=new ContentValues();

        values.put("date","7/21/2017");
        values.put("message","get to work");
        notidb.insert("notitable2",null,values);
        Toast.makeText(getApplicationContext(),"first entry", Toast.LENGTH_LONG).show();


        values.put("date","7/22/2017");
        values.put("message", "reach home");
        notidb.insert("notitable2",null,values);
        Toast.makeText(getApplicationContext(), "second entry", Toast.LENGTH_LONG).show();

        values.put("date","7/23/2017");
        values.put("message","science the shit out of it");
        notidb.insert("notitable2",null,values);
        Toast.makeText(getApplicationContext(), "third entry", Toast.LENGTH_LONG).show();

        values.put("date","7/24/2017");
        values.put("message","get to work yet again");
        notidb.insert("notitable2",null,values);
        Toast.makeText(getApplicationContext(), "fourth entry", Toast.LENGTH_LONG).show();


        String cols[]={"date"};
        String msg[]={"message"};
        Cursor c=notidb.query("notitable2",cols, null, null, null, null, null);
        Cursor d=notidb.query("notitable2",msg,null, null, null, null, null);

      //  c.getPosition
        // c.getString();

        //d.moveToNext();


        d.moveToNext();
        Log.e("check",d.getString(0));

         while(c.moveToNext()){



               int x= date.compareTo(c.getString(0));
           // Toast.makeText(MainActivity.this," "+x,Toast.LENGTH_SHORT).show();

             if(x!=0){

                 d.moveToNext();

                 continue;

             }
            if(x==0){

               // Toast.makeText(MainActivity.this,d.getString(0),Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this,MainActivity.class);
                PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);
                Notification notification=new Notification.Builder(MainActivity.this)
                        .setContentTitle("Noti Daily")
                        .setContentText(d.getString(0))
                        .setSmallIcon(R.drawable.a)
                        .setContentIntent(pendingIntent).getNotification();

                notification.flags=Notification.FLAG_AUTO_CANCEL;

                NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(0,notification);



                    c.moveToFirst();
                    d.moveToFirst();
                    d.moveToNext();

                continue;
            }

        }


       // notificationManager.notify(0,notification);


    }

}

1 个答案:

答案 0 :(得分:0)

如果您想在特定时间每天显示通知,可以使用重复闹钟。

Click here to know more about repeating alarm

或者,如果您想在不同时间显示每日通知,请不要设置重复闹钟。然后从数据库中设置第一个日期的警报,当收到通知时,从数据库获取下一个日期并设置下一个警报等等。