Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中实现通知的问题_Android_Android Layout_Android Intent_Android Alarms_Android Calendar - Fatal编程技术网

在android中实现通知的问题

在android中实现通知的问题,android,android-layout,android-intent,android-alarms,android-calendar,Android,Android Layout,Android Intent,Android Alarms,Android Calendar,我正在尝试在android中创建一个提醒,在那里我们可以设置一个通知,在那里我们可以选择日期和时间并相应地设置提醒。我无法检测到我的错误。请帮助 MainActivity.java public class MainActivity extends Activity { // This is a handle so that we can call methods on our service private ScheduleClient scheduleClient;

我正在尝试在android中创建一个提醒,在那里我们可以设置一个通知,在那里我们可以选择日期和时间并相应地设置提醒。我无法检测到我的错误。请帮助

MainActivity.java

public class MainActivity extends Activity  {
    // This is a handle so that we can call methods on our service
    private ScheduleClient scheduleClient;
    // This is the date picker used to select the date for our notification
    private DatePicker picker;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create a new service client and bind our activity to this service
        scheduleClient = new ScheduleClient(this);
        scheduleClient.doBindService();

        // Get a reference to our date picker
        picker = (DatePicker) findViewById(R.id.scheduleTimePicker);
    }

    /**
     * This is the onClick called from the XML to set a new notification 
     */
    public void onDateSelectedButtonClick(View v){
        // Get the date from our datepicker
        int day = picker.getDayOfMonth();
        int month = picker.getMonth();
        int year = picker.getYear();
        // Create a new calendar set to the date chosen
        // we set the time to midnight (i.e. the first minute of that day)
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        // Ask our service to set an alarm for that date, this activity talks to the client that talks to the service
        scheduleClient.setAlarmForNotification(c);
        // Notify the user what they just did
        Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStop() {
        // When our activity is stopped ensure we also stop the connection to the service
        // this stops us leaking our activity into the system *bad*
        if(scheduleClient != null)
            scheduleClient.doUnbindService();
        super.onStop();
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <DatePicker
        android:id="@+id/scheduleTimePicker"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/selectButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onDateSelectedButtonClick"
        android:text="Set notification for this date" />

</LinearLayout>
ScheduleService.java

public class ScheduleService extends Service {

    /**
     * Class for clients to access
     */
    public class ServiceBinder extends Binder {
        ScheduleService getService() {
            return ScheduleService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("ScheduleService", "Received start id " + startId + ": " + intent);

        // We want this service to continue running until it is explicitly stopped, so return sticky.
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // This is the object that receives interactions from clients. See
    private final IBinder mBinder = new ServiceBinder();

    /**
     * Show an alarm for a certain date when the alarm is called it will pop up a notification
     */
    public void setAlarm(Calendar c) {
        // This starts a new thread to set the alarm
        // You want to push off your tasks onto a new thread to free up the UI to carry on responding
        new AlarmTask(this, c).run();
    }
}
    }
AlarmTask.java

  public class AlarmTask implements Runnable{
        // The date selected for the alarm
        private final Calendar date;
        // The android system alarm manager
        private final AlarmManager am;
        // Your context to retrieve the alarm manager from
        private final Context context;

        public AlarmTask(Context context, Calendar date) {
            this.context = context;
            this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            this.date = date;
        }

        @Override
        public void run() {
            // Request to start are service when the alarm date is upon us
            // We don't start an activity as we just want to pop up a notification into the system bar not a full activity
            Intent intent = new Intent(context, NotifyService.class);
            intent.putExtra(NotifyService.INTENT_NOTIFY, true);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

            // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
            am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
        }
    }

NotifyService.java

public class NotifyService extends Service {

    /**
     * Class for clients to access
     */
    public class ServiceBinder extends Binder {
        NotifyService getService() {
            return NotifyService.this;
        }
    }

    // Unique id to identify the notification.
    private static final int NOTIFICATION = 123;
    // Name of an intent extra we can use to identify if this service was started to create a notification  
    public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY";
    // The system notification manager
    private NotificationManager mNM;

    @Override
    public void onCreate() {
        Log.i("NotifyService", "onCreate()");
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);

        // If this service was started by out AlarmTask intent then we want to show our notification
        if(intent.getBooleanExtra(INTENT_NOTIFY, false))
            showNotification();

        // We don't care if this service is stopped as we have already delivered our notification
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // This is the object that receives interactions from clients
    private final IBinder mBinder = new ServiceBinder();

    /**
     * Creates a notification and shows it in the OS drag-down status bar
     */
    private void showNotification() {
        // This is the 'title' of the notification
        CharSequence title = "Alarm!!";
        // This is the icon to use on the notification
        int icon = R.drawable.ic_dialog_alert;
        // This is the scrolling text of the notification
        CharSequence text = "Your notification time is upon us.";       
        // What time to show on the notification
        long time = System.currentTimeMillis();

        Notification notification = new Notification(icon, text, time);

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, title, text, contentIntent);

        // Clear the notification when it is pressed
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Send the notification to the system.
        mNM.notify(NOTIFICATION, notification);

        // Stop the service when we are finished
        stopSelf();
    }
}
AndroidManifest.java

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.reminder.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>
        <activity
            android:name="com.example.reminder.SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>

        <service android:name=".service.ScheduleService" />
        <service android:name=".service.NotifyService" />
    </application>

</manifest>

请提供一些建议……

ScheduleClient中第54行的内容是什么?public void setAlarmForNotificationCalendar c{mBoundService.setAlarmc;}尝试在服务断开和服务连接的日志中打印一些内容。看看流程的走向什么东西都被打印出来了。。。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.reminder"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.reminder.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>
        <activity
            android:name="com.example.reminder.SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>

        <service android:name=".service.ScheduleService" />
        <service android:name=".service.NotifyService" />
    </application>

</manifest>
08-11 07:01:02.900: E/AndroidRuntime(817): FATAL EXCEPTION: main
08-11 07:01:02.900: E/AndroidRuntime(817): Process: com.example.reminder, PID: 817
08-11 07:01:02.900: E/AndroidRuntime(817): java.lang.IllegalStateException: Could not execute method of the activity
08-11 07:01:02.900: E/AndroidRuntime(817):  at android.view.View$1.onClick(View.java:3823)
08-11 07:01:02.900: E/AndroidRuntime(817):  at android.view.View.performClick(View.java:4438)
08-11 07:01:02.900: E/AndroidRuntime(817):  at android.view.View$PerformClick.run(View.java:18422)
08-11 07:01:02.900: E/AndroidRuntime(817):  at android.os.Handler.handleCallback(Handler.java:733)
08-11 07:01:02.900: E/AndroidRuntime(817):  at android.os.Handler.dispatchMessage(Handler.java:95)
08-11 07:01:02.900: E/AndroidRuntime(817):  at android.os.Looper.loop(Looper.java:136)
08-11 07:01:02.900: E/AndroidRuntime(817):  at android.app.ActivityThread.main(ActivityThread.java:5017)
08-11 07:01:02.900: E/AndroidRuntime(817):  at java.lang.reflect.Method.invokeNative(Native Method)
08-11 07:01:02.900: E/AndroidRuntime(817):  at java.lang.reflect.Method.invoke(Method.java:515)
08-11 07:01:02.900: E/AndroidRuntime(817):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-11 07:01:02.900: E/AndroidRuntime(817):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-11 07:01:02.900: E/AndroidRuntime(817):  at dalvik.system.NativeStart.main(Native Method)
08-11 07:01:02.900: E/AndroidRuntime(817): Caused by: java.lang.reflect.InvocationTargetException
08-11 07:01:02.900: E/AndroidRuntime(817):  at java.lang.reflect.Method.invokeNative(Native Method)
08-11 07:01:02.900: E/AndroidRuntime(817):  at java.lang.reflect.Method.invoke(Method.java:515)
08-11 07:01:02.900: E/AndroidRuntime(817):  at android.view.View$1.onClick(View.java:3818)
08-11 07:01:02.900: E/AndroidRuntime(817):  ... 11 more
08-11 07:01:02.900: E/AndroidRuntime(817): Caused by: java.lang.NullPointerException
08-11 07:01:02.900: E/AndroidRuntime(817):  at com.example.reminder.ScheduleClient.setAlarmForNotification(ScheduleClient.java:54)
08-11 07:01:02.900: E/AndroidRuntime(817):  at com.example.reminder.MainActivity.onDateSelectedButtonClick(MainActivity.java:46)
08-11 07:01:02.900: E/AndroidRuntime(817):  ... 14 more