Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Status bar notification every day at time
    primarykey
    data
    text
    <p>In my app, users have to fill in 9 different times with timepickers. These times are stored in a SQLite database. I want that users get a status bar notification when one of the times has been reached.</p> <p>Example:</p> <p>Users fill the first time in at 09:00. If it is 09:00 in real time, users need to get a statusbar notification. I want that they don't need to start the app, it has to be automatic.</p> <p>After spending more then 4 hours today at stackoverflow and other websites, I didn't succeed. I have tried the following code, but I didn't succeed. The notification is now set to 12:00. When the time reached 12:00, I didn't get a message.</p> <p>MainActivity:</p> <pre><code> public class MainActivity extends Activity { AlarmManager am; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent alarmIntent = new Intent(this, TimeAlarm.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 12); calendar.set(Calendar.MINUTE, 00); calendar.set(Calendar.SECOND, 00); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pendingIntent); } } </code></pre> <p>TimeAlarm.class:</p> <pre><code>public class TimeAlarm extends BroadcastReceiver { NotificationManager nm; @Override public void onReceive(Context context, Intent intent) { Intent startServiceIntent = new Intent(context, NotificationService.class); context.startService(startServiceIntent); } } </code></pre> <p>NotificationService.class:</p> <pre><code>public class NotificationService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { handleIntent(intent); return START_NOT_STICKY; } private NotificationManager nm; private WakeLock mWakeLock; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onDestroy() { super.onDestroy(); mWakeLock.release(); } private void showNotification() { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Test") .setContentText("Test message"); Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(1, mBuilder.build()); } private class PollTask extends AsyncTask&lt;Void, Void, Void&gt; { @Override protected Void doInBackground(Void... params) { showNotification(); return null; } @Override protected void onPostExecute(Void result) { stopSelf(); } } private void handleIntent(Intent intent) { PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NotificationsService"); mWakeLock.acquire(); ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); if (!cm.getBackgroundDataSetting()) { stopSelf(); return; } new PollTask().execute(); } </code></pre> <p>AndroidManifest:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.app" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt; &lt;uses-permission android:name="com.test.app.permission.C2D_MESSAGE" /&gt; &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt; &lt;!-- This app has permission to register and receive data message. --&gt; &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" &gt; &lt;activity android:name="com.test.app.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;receiver android:name="com.test.app.TimeAlarm"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload