1

アプリで通知バーを作成していますが、これが私のコードです:

主な活動:

package com.example.notificationservicedemo;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends Activity implements OnClickListener {

    EditText editMsg;
    DatePicker datePicker;
    TimePicker timePicker;
    Button btnSetNotification;

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

        editMsg = (EditText) findViewById(R.id.editText1);
        datePicker = (DatePicker) findViewById(R.id.datePicker1);
        timePicker = (TimePicker) findViewById(R.id.timePicker1);
        btnSetNotification = (Button) findViewById(R.id.buttonSetNotification);

        btnSetNotification.setOnClickListener(this);
    }

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

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        // TODO Auto-generated method stub
        switch(view.getId()){
        case R.id.buttonSetNotification:
            String message=editMsg.getText().toString();
            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.MONTH, datePicker.getMonth());
            calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
            calendar.set(Calendar.YEAR, datePicker.getYear());

            calendar.set(Calendar.HOUR, timePicker.getCurrentHour());
            calendar.set(Calendar.MINUTE, timePicker.getCurrentHour());


            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


            intent.setClass(this, MyNotificationService.class);
            PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            //startService(intent);

            break;


        }
    }

}

MyNotificationService:

package com.example.notificationservicedemo;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyNotificationService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "OnCreate()", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "OnDestroy()", Toast.LENGTH_SHORT).show();
    }

    @Override
    @Deprecated
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        Toast.makeText(this, "OnStart()", Toast.LENGTH_SHORT).show();
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent= new Intent(this,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification notification = new Notification(R.drawable.ic_launcher, "Bla bla bla", System.currentTimeMillis());

        String contentTitle="Title";
        String contentText="This is your message";
        notification.setLatestEventInfo(this, contentTitle, contentText, pendingIntent);
        notificationManager.notify(123, notification);
    }



}

マニフェスト:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notificationservicedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.notificationservicedemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service  android:name="com.example.notificationservicedemo.MyNotificationService"></service>
    </application>

</manifest>

問題は、通知が表示される必要があるときに時間を選択するときにボタンを押して保存すると、すぐに通知が表示され、timePicker と datePicker が無視されることです。問題とは何ですか?

4

1 に答える 1