Olá pessoal!
A app que estou a desenvolver tem de mandar todas as segunda-feiras às 12:00h uma notificação.
Mas em vez de fazer isso ela envia a notificação cada vez que abro a app e manda varias vezes a notificação sem ser quando deve.
Eu já vi a documentação oficial e em varios forums mas não consigo perceber o que estou a fazer mal!
Este é o meu código até agora.
MainActivity.class
public class MainActivity extends Activity { // Dialogs variables private static final int DIALOG_ABOUT = 0; private static final int DIALOG_PROGRESSBAR = 1; private static final int DIALOG_AFTER_DOWNLOAD = 2; private static final int DIALOG_AFTER_DOWNLOAD2 = 3; private ProgressDialog progressDialog; OutputStream output; private static long back_pressed; public static boolean active; private PendingIntent pendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** * WEEK NOTIFICATION CODE */ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_WEEK, 2); calendar.set(Calendar.HOUR_OF_DAY, 12); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.AM_PM, Calendar.PM); AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE); pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(this, NotificationReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 7 * AlarmManager.INTERVAL_DAY, pendingIntent); /** * END OF WEEK NOTIFICATION CODE */
AlarmService.class
public class AlarmService extends Service { String notificationText = "New files updates available!"; int startID_ = 123; @Override public int onStartCommand(Intent intent,int flags, int startID_) { Intent resultIntent = new Intent(this, NotificationActivity.class); Notification.Builder notification = new Notification.Builder(this); notification.setSmallIcon(R.drawable.ic_launcher); notification.setContentTitle(getString(R.string.app_name)); notification.setContentText(notificationText); notification.setDefaults(Notification.DEFAULT_SOUND); notification.setDefaults(Notification.DEFAULT_VIBRATE); notification.setDefaults(Notification.DEFAULT_LIGHTS); resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); resultIntent.putExtra("notify", notificationText); resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(NotificationActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); notification.setContentIntent(resultPendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(startID_, notification.build()); return START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { super.onDestroy(); }}
NotificationReceiver.class
public class NotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent service1 = new Intent(context, AlarmService.class); context.startService(service1); }}
AndroidManifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="pt.diogoapps.trigofoods" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="19" /> <!-- GCM connects to Google Services. --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- GCM requires a Google account. --> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.VIBRATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/trigofoodsTheme" > <activity android:name="pt.diogoapps.trigofoods.MainActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SavedFilesActivity" android:screenOrientation="portrait"/> <activity android:name=".NotificationActivity" android:theme="@style/trigofoodsTheme.Dialog"/> <activity android:name=".BrowserActivity" android:screenOrientation="portrait"/> <service android:name="pt.diogoapps.trigofoods.others.AlarmService" android:enabled="true"/> <receiver android:name="pt.diogoapps.trigofoods.others.NotificationReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application>
Espero que alguem me consiga ajudar, obrigado!
Editado por Digas, 22 November 2013 - 17:48.