So I don't know why this code is not working. I wanted to make an "alarm" notification that would go off once a day. Just wanted to say I'm new to android. Changed up the code a bit couple of times but still wont work.
Alarm method executes notification does too but I get this : -248/? D/PowerManagerService﹕ releaseWakeLock flags=0x1 tag=AlarmManager W/ActivityManager﹕ Unable to start service Intent { flg=0x4 cmp=com.example.polakken.test/.lol (has extras) }: not found 06-13 00:00:00.825 231-267/? D/PowerManagerService﹕ acquireWakeLock flags=0x1 tag=AlarmManager 06-13 00:00:00.825 231-248/? D/PowerManagerService﹕ releaseWakeLock flags=0x1 tag=AlarmManager
My Code:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
int i = preferences.getInt("numberoflaunches", 1);
if (i < 2) {
alarmMethod();
i++;
editor.putInt("numberoflaunches", i);
editor.commit();
}
if (savedInstanceState == null) {
splashMethod();
}
}
//...
private void alarmMethod() {
Intent intentbro = new Intent(this, lol.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intentbro, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);
Toast.makeText(MainActivity.this, "start alarm", Toast.LENGTH_LONG).show();
}
//notification class
public class lol extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent1, 0);
b.setContentText("lol");
b.setContentTitle("Default notification");
b.setSmallIcon(R.drawable.iconography_small_size);
b.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
}
Follow these steps
1Create an Activity and add this code to setAlarm(can be any name)
Suppose you want to set Alarm for 24/02/15 ,14:00PM
2)Inside BroadcastReceiver onReceive() method
}
3)Declare Two permissions in Manifest.xml
4)Declare You AlarmReceiver in Manifest.xml
Follow these documents http://developer.android.com/reference/android/content/BroadcastReceiver.html
http://developer.android.com/reference/android/app/AlarmManager.html
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
http://developer.android.com/reference/android/os/PowerManager.WakeLock.html
Hope this will help you
You are creating the
PendingIntent
withPendingIntent.getService()
, but you are supplying anIntent
with anActivity
.So for your code to work you would need to use
PendingIntent.getActivity()
. (Additionally you have to add the corresponding<activity>
tag to yourAndroidManifest.xml
.)However, this is probably not exactly what you want: The only thing the
lol
Activity does is adding a notification. You might want to use aBroadcastReceiver
(or perhaps aWakefulBroadcastReceiver
) instead. In this case you would need to use PendingIntent.getBroadcast() instead ofgetActivity()
.