Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.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_Notifications - Fatal编程技术网

基于检查的Android每日通知

基于检查的Android每日通知,android,notifications,Android,Notifications,我目前正在开发一个Android应用程序,它可以记住用户的日常行为。 其想法是,如果用户尚未执行操作,应用程序会向用户发出通知。通知应在每天下午3点出现 我的问题是,我如何实施检查。我已经找到了关于如何使用AlarmManager和NotificationManager的教程,但即使用户已经完成了,这些教程也会记住用户。 是否有办法开始检查而不是通知,并且如果检查表明应该有通知,则会发布通知?您可以保存一个共享首选项,该首选项指示用户上次完成日常操作的时间。当您的警报触发时,您将启动一个检查共享

我目前正在开发一个Android应用程序,它可以记住用户的日常行为。 其想法是,如果用户尚未执行操作,应用程序会向用户发出通知。通知应在每天下午3点出现

我的问题是,我如何实施检查。我已经找到了关于如何使用AlarmManager和NotificationManager的教程,但即使用户已经完成了,这些教程也会记住用户。
是否有办法开始检查而不是通知,并且如果检查表明应该有通知,则会发布通知?

您可以保存一个共享首选项,该首选项指示用户上次完成日常操作的时间。当您的警报触发时,您将启动一个检查共享首选项状态的服务。如果不到1天前,那么用户今天已经完成了日常操作

以下是您检查的方式:

SharedPreferences settings = getSharedPreferences(MainActivity.PREFS, MODE_PRIVATE);
if (System.currentTimeMillis() - settings.getLong("lastTimeActionDone", 0) < MILLISECS_PER_DAY) {
    // Action done within last day
} else {        
    // Action not done within last day
}
编辑: 下面是一个示例应用程序,它可以满足您的大部分需求。如前所述,它只是在每天的同一时间醒来,该时间是应用程序最后一次启动的时间。因此,缺少的一个主要部分是让闹钟在每天的特定时间唤醒。要使该部分正常工作,您需要在设置(第一次)报警时间时使用一些日期时间算法

MainActivity.java: CheckActionDone.java: main_activity.xml:

strings.xml:

回来
行动
启用通知
禁用通知
AndroidManifest.xml:


这是我的计划,但我如何检查它是否已设置?我可以发布示例代码来执行警报、服务和通知。谢谢。我已经有了一些类似的代码,我的问题是调用检查,但是如果我理解正确,我可以编写一个包含此检查的活动,并通过AlarmManager调用此活动。是这样吗?你能提供一些简单的代码吗?
SharedPreferences settings = getSharedPreferences(MainActivity.PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = null;

editor.putLong("lastTimeActionDone", System.currentTimeMillis());
editor.commit();        
package com.example.dailycheck;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {

    private final static String TAG = "MainActivity";
    public final static String PREFS = "PrefsFile";

    private SharedPreferences settings = null;
    private SharedPreferences.Editor editor = null;

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

        // Save time of run:
        settings = getSharedPreferences(PREFS, MODE_PRIVATE);
        editor = settings.edit();

        // First time running app?
        if (!settings.contains("lastTimeActionDone"))
            enableNotification(null);

        Log.v(TAG, "Starting CheckRecentRun service...");
        startService(new Intent(this, CheckActionDone.class));
    }

    public void doAction(View v) {
        Log.v(TAG, "Recording time action done");
        editor.putLong("lastTimeActionDone", System.currentTimeMillis());
        editor.commit();        
    }

    public void enableNotification(View v) {
        editor.putBoolean("enabled", true);                
        editor.commit();        
        Log.v(TAG, "Notifications enabled");
    }

    public void disableNotification(View v) {
        editor.putBoolean("enabled", false);                
        editor.commit();        
        Log.v(TAG, "Notifications disabled");
    }
}
package com.example.dailycheck;

import android.app.AlarmManager;
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.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;

public class CheckActionDone extends Service {

    private final static String TAG = "CheckRecentPlay";
    private static Long MILLISECS_PER_DAY = 86400000L;

    private static long delay = 60000;                 // 1 minute (for testing)
//    private static long delay = MILLISECS_PER_DAY;   // 1 day

    @Override
    public void onCreate() {
        super.onCreate();

        Log.v(TAG, "Service started");                
        SharedPreferences settings = getSharedPreferences(MainActivity.PREFS, MODE_PRIVATE);

        // Are notifications enabled?
        if (settings.getBoolean("enabled", true)) {
            // And was action not recently done?
            Long lastTimeDone = settings.getLong("lastTimeActionDone", 0);
            if ((System.currentTimeMillis() - lastTimeDone) >= delay) {
                sendNotification();
             } else {
                 Log.i(TAG, "Action recently done");
             }
        } else {        
            Log.i(TAG, "Notifications are disabled");
        }

        // Set an alarm for the next time this service should run:
        setAlarm();

        Log.v(TAG, "Service stopped");        
        stopSelf();
    }

    public void setAlarm() {

        Intent serviceIntent = new Intent(this, CheckActionDone.class);
        PendingIntent pi = PendingIntent.getService(this, 131313, serviceIntent,
                                                    PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pi);
        Log.v(TAG, "Alarm set");        
    }

    public void sendNotification() {

        Intent mainIntent = new Intent(this, MainActivity.class);
        @SuppressWarnings("deprecation")
        Notification noti = new Notification.Builder(this)
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(this, 131314, mainIntent,
                              PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("Action Not Do")
            .setContentText("You didn't do the daily action.")
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("You haven;t done the daily action; please do it now.")
            .setWhen(System.currentTimeMillis())
            .getNotification();

        NotificationManager notificationManager
            = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(131315, noti);

        Log.v(TAG, "Notification sent");        
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="doAction"
        android:text="@string/do_action" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="enableNotification"
        android:text="@string/enable" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="disableNotification"
        android:text="@string/disable" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Come Back</string>
    <string name="do_action">Do Action</string>
    <string name="enable">Enable Notifications</string>
    <string name="disable">Disable Notifications</string>

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

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

    <uses-permission android:name="android.permission.VIBRATE" />

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

</manifest>