问题 Android - 如何将通知设置为将来的特定日期?


编辑:已解决!曾经想要从某个特定日期开始设置通知(当某个活动开始时或按下按钮时?)阅读更多内容以了解如何:

 //Set a notification in 7 days
                Calendar sevendayalarm = Calendar.getInstance();

                sevendayalarm.add(Calendar.DATE, 7);

                Intent intent = new Intent(this, Receiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 001, intent, 0);

                AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, sevendayalarm.getTimeInMillis(), pendingIntent);

这是Receiver类的代码

public class Receiver extends Service {

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

    @Override
    public void onCreate() {
        Intent intent = new Intent(this, Test.class);
        long[] pattern = {0, 300, 0};
        PendingIntent pi = PendingIntent.getActivity(this, 01234, intent, 0);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.depressiontest)
            .setContentTitle("Take Questionnaire")
            .setContentText("Take questionnaire for Duke Mood Study.")
            .setVibrate(pattern)
            .setAutoCancel(true);

        mBuilder.setContentIntent(pi);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(01234, mBuilder.build());
    }
}

并且不要忘记在清单中添加以下权限!

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
 <service android:name=".Receiver2" android:enabled="true">
        <intent-filter> <action android:name="NOTIFICATION_SERVICE" /></intent-filter>
    </service>

10892
2018-01-19 04:24


起源

您如何以及在何处收到警报?你可以发布代码吗? - Skynet
当你转发警报的日期时? - W I Z A R D
@Skynet刚添加了接收器的代码! - eddielement
你有没有在清单中提到你的接收器和服务? - Android Noob
发布广播接收器的代码 - Prabhakar


答案:


不要忘记给出以下清单权限

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

您的广播接收器注册将是这样的

   <receiver android:name=".AlarmReceiver" >
         <intent-filter>
           <action android:name="NOTIFICATION_SERVICE" />
         </intent-filter>
     </receiver>

9
2018-01-19 05:02



不,我只有这个:<receiver android:name =“。接收器”android:enabled =“true”/> - eddielement
我更新了我的答案..你能检查一次吗? - King of Masses


需要注意的一件重要事项: 使用注册广播接收器时 Intent-Filter,你需要添加 exported 属性并将其设置为false。喜欢这个:

<service

        android:name=".utility.AlarmReceiver" android:exported="false">
        <intent-filter>
            <action android:name="NOTIFICATION_SERVICE" />
        </intent-filter>

    </service>

其他应用程序的其他组件将能够调用您的服务或与您的服务进行交互。

完整的解释 谷歌

android:exported

   Specifies whether or not components of other applications
   can invoke the service or interact with it —
   "true" if they can, and "false" if not.

When the value is "false", only components of the same application or    
applications with the same user ID can start the service or bind to it.

The default value depends on whether the service contains intent filters.      
The absence of any filters means that it can be invoked only by specifying 
its exact class name. This implies that the service is intended only for 
application-internal use (since others would not know the class name). So 
in this case, the default value is "false". On the other hand, the presence 
of at least one filter implies that the service is intended for external 
use, so the default value is "true".

2
2017-07-24 07:26