0

ボタンのタップで通知を表示する必要がありますが、その時点ではなく、いつかのようにしばらくしてから表示する必要があります。そのためのコードを記述しましたが、出力が表示されません。

どこが間違っているのか、何を追加すれば機能するのかを確認して教えてください。

これは私の MainActivity です:

public class MainActivity extends Activity {

    private ImageButton Button1;
    private int year;
    private int month;
    private int date;
    private int hour;
    private int minute;
    private int second;

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

        Button1 = (ImageButton) findViewById(R.id.Button1);
        Button1.setTag("ON");
        //Button1.setSelected(selected);
        Button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

        if (v.getTag().toString().equals("ON")) {
                    v.setTag("OFF");
                     v.setBackgroundResource(R.drawable.star_off);
                    // NotificationHandler notification = new NotificationHandler(MainActivity.this);

                    //notification.sendNotification("New Notification Received");
                    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    //Notification notification = new Notification(R.drawable.icon, "A New Message!", System.currentTimeMillis());
                }else {
                    v.setTag("ON");
                     v.setBackgroundResource(R.drawable.star_on);
                }


            }
        });
    }



    public void startAlarm() {
        @SuppressWarnings("static-access")
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
        Calendar calendar =  Calendar.getInstance();
        calendar.set(2013, 11,13, 8,  30,  1);
        long when = calendar.getTimeInMillis();         // notification time
                Intent intent = new Intent(this, ReminderService.class);
                PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
                alarmManager.set(AlarmManager.RTC, when, pendingIntent);
            }



    public class ReminderService extends IntentService {
        private static final int NOTIF_ID = 1;

        public ReminderService(){
            super("ReminderService");
        }

        @SuppressWarnings({ "deprecation", "static-access" })
        @Override
          protected void onHandleIntent(Intent intent) {
            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            long when = System.currentTimeMillis();         // notification time

            Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags |= notification.FLAG_AUTO_CANCEL;
            Intent notificationIntent = new Intent(this, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
            notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
            nm.notify(NOTIF_ID, notification);
        }

    }



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

}

これは私の通知ハンドラクラスです:

public class NotificationHandler {


    Context context = null;
    public int contentinfo = 1;


    public NotificationHandler(Context context){

        this.context = context;
    }

    //Send Notification start

    public void sendNotification(String message) {

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

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

        alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


            contentinfo++;
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("New Message")
            .setContentText("messgae")
            .setContentInfo(""+contentinfo)
            .setSound(alarmSound)
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.InboxStyle());
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(context, MainActivity.class);


            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                            0,
                            PendingIntent.FLAG_UPDATE_CURRENT
                            );
            mBuilder.setContentIntent(resultPendingIntent);

            // mId allows you to update the notification later on.
            int mId = 1;
            notificationManager.notify(mId, mBuilder.build());



        //Send Notification end


    }
}

ありがとう。

4

1 に答える 1

0

完全な通知コードを実行しませんでした。まず第一に、コードで startAlarm() 関数を呼び出していません。

onClick() から setAlarm() を呼び出します。

于 2013-11-13T15:49:38.650 に答える