I'm trying to configure an alarm notification in Android and unfortunately no alarm is generated.
Here is the creation of the alarm:
myDate = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(time_Date_str);
t.setDueDate(myDate);
t.setHasDate(true);
Intent alarmNotificationIntent = new Intent(this, ReminderNotification.class);
alarmNotificationIntent.putExtra("task", t);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, (int) task_id, alarmNotificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t.getDueDate().getTime());
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
In addition I've created:
NotificationReceiver
import android.app.Activity;
import android.os.Bundle;
public class NotificationReceiver extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
ReminderNotification
import android.content.Context;
import android.content.Intent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
public class ReminderNotification extends BroadcastReceiver
{
public ReminderNotification()
{
// TODO Auto-generated constructor stub
}
#Override
public void onReceive(Context context, Intent intent) {
// The PendingIntent to launch our activity if the user selects this notification
Task task = (Task)intent.getSerializableExtra("task");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
Intent snzInt = new Intent(context, SnoozeReminderReceiver.class);
snzInt.putExtra("task", task);
PendingIntent snoozeIntent = PendingIntent.getBroadcast(context, 0,snzInt, PendingIntent.FLAG_CANCEL_CURRENT);
Intent doneInt = new Intent(context,DoneActionReceiver.class);
doneInt.putExtra("task", task);
PendingIntent doneIntent = PendingIntent.getBroadcast(context, 0,doneInt, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
The manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="il.ac.she.dd.todoli"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<receiver android:name="ReminderNotification"/>
<receiver android:name="SnoozeReminderReceiver"/>
<receiver android:name="DoneActionReceiver"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".ListNodeActivity"
android:label="#string/title_activity_list_node"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="il.ac.shenkar.david.todolistex2.MainActivity" />
</activity>
<activity
android:name=".Signup_Activity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"></activity>
<activity
android:name=".create_team"
android:label="Create new task team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".invite_member"
android:label="Invite Members Your Team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Login_activity"
android:label="Login to Wiggle"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EditTaskActivity"
android:label="Edit Task"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
I have a feeling that I'm missing something very small but cannot find the issue.
Please let me know what I'm doing wrong.
Thanks in advance.
you have to first build notification and then notify it...
NotificationManager _manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle("New")
.setContentText("hello")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pintent)
.build();
notification.vibrate=new long[]{100,250,100,500};
_manager.notify(notification_id, notification);
You can find a complete example of Alarm and Notification in my this GitHub Repo
Related
Not opening specific activity on notification click when the app is in background/not running
The notification-click starts specified activity only when the app is opened up and the notification-click is performed. If the app is in background/not running and the notification-click is performed, the application's MainActivity opens up. In short, it is like the app opens normally following the activity stack instead of opening the specified activity in the PendingIntent.
I saw a solution similar to this problem in this forum did not help unfortunately
MyApplication
import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONException;
import org.json.JSONObject;
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this).setNotificationOpenedHandler(new ExampleOpenHandler())
.autoPromptLocation(true).init();
}
private void startNotification() { Log.i("NextActivity", "startNotification");
// Sets an ID for the notification int mNotificationId = 001;
// Build Notification , setOngoing keeps the notification always in status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.bugununfaaliyet)
.setContentTitle("Stop LDB")
.setContentText("Click to stop LDB")
.setOngoing(true);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(1, mBuilder.build());
}
private class ExampleOpenHandler implements OneSignal.NotificationOpenedHandler {
#Override
public void notificationOpened(OSNotificationOpenResult result) {
JSONObject data=result.notification.payload.additionalData;
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
try{
intent.putExtra("datam",data.getString("1"));
}catch (JSONException e) {
e.printStackTrace();
}
startActivity(intent);
}
}
}
SecondActivity
package blabla.com.blabla;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView dataview;
String getdata;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getdata = getIntent().getExtras().getString("datam");
dataview = (TextView) findViewById(R.id.dataview);
dataview.setText(getdata);
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="blabla.com.blabla">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Activity.girisKullaniciAdi"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity.TabbedActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.KameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.MainActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.Anasayfa" />
<activity
android:name=".Activity.FizikselSayimKameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.EkipmanGecmisiKameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.EkipmanGecmisiActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.YeniIsEmriKameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT"
android:value="DISABLE" />
</provider>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
Please check out this answer..
private void startNotification() {
Log.i("NextActivity", "startNotification");
// Sets an ID for the notification
int mNotificationId = 001;
// Build Notification , setOngoing keeps the notification always in status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ldb)
.setContentTitle("Stop LDB")
.setContentText("Click to stop LDB")
.setOngoing(true);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
// StopScript is your Activity Name
I am trying to use alarm manager to show notification after 5 sec. I have tried many sites but was not able to understand please give a simple example for explaining how to use alarm manager and connect notification with it.
I am newbie.
this is function i used to set alarm and I am not getting notification after 5 secs actually not at all not in emulator nor in android mobile. If put code to create notififcation with a button pressing that is working great.
public void setAlarm(View view)
{
Intent alertIntent = new Intent(this, AlertReciver.class);
Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
tv.setText("completed");
}
and this class to make it work
public class AlertReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
createNotification(context,"times Up", "5 SEcond has passed", "Alert");
}
public void createNotification(Context context,String msg, String msgText, String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ne)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
this is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deepaksingh.goinnotifying" >
<uses-permission android:name="android.permission.alarm.permission.SET_ALARM" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</application>
You havn't set permission for wake:
<uses-permission android:name="android.permission.WAKE_LOCK" />
ANd you didn't register your recevier.Here.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<receiver android:name=".AlertReciver "/>
</application>
I'm making an alarm receiver, but the alarm is not triggered.
This is my code :
Start alarm in MainActivity :
private void setupAlarm(){
Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);
Log.e("setupAlarm", "Setup alarm complete");
}
Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phonelogger"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
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="SettingActivity" android:label="#string/action_settings" ></activity>
<service android:name="com.logdata.LogManager" />
<receiver
android:name="com.logdata.AlarmReceiver"
android:enabled="true">
</receiver>
</application>
AlarmReceiver :
package com.logdata;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, com.logdata.LogManager.class);
context.startService(myIntent);
Log.e("AlarmReceiver", "Get message");
}
}
Can someone tell me what the problem is ?
Your Intent is used to explicitly start the AlarmReceiver class which is a BroadcastReceiver. Hence, you need to use getBroadcast(), not getService(), to create the PendingIntent object.
Replace
PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
with
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Try this. This will work.
Try To Use THis. It will Work for you
private void setupAlarm(){
Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);
Log.e("setupAlarm", "Setup alarm complete");
}
I got an BroadcastReceiver which contains the following code:
package de.my.app;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent alarmIntent = new Intent(context,AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Log.e("RECEIVER", "WORKS");
}
}
In my log the log message of the Receiver shows up, but the message from the service doesn't show up.
If it would work fine it would show up every 10 seconds.
This is my AlarmService class:
package de.my.app;
import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class AlarmService extends IntentService {
public AlarmService(
) {
super("AlarmService");
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onHandleIntent(Intent intent) {
Log.e("SERVICE","WORKS");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY_COMPATIBILITY;
}
#Override
public void onDestroy() {
super.onDestroy();
sendBroadcast(new Intent("restart"));
}
}
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.my.app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<permission
android:name="de.my.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="de.my.app.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MyActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="Einstellungen">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.my.app.MyActivity" />
</activity>
<activity
android:name=".Start"
android:label="#string/app_name" />
<receiver
android:name=".GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.hmkcode.android.gcm" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="restart" />
</intent-filter>
</receiver>
<service android:name=".AlarmService" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
Instead of using getBroadcast(...) in the following line...
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
...you should be using getService(...).
Simply put - if you want a PendingIntent to be used for an Activity, Service or a Broadcast, you need to use the correct method depending on which type of app component it is.
Try...
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
i am doing this way
//class ScheduledServiceDemoActivity
public class ScheduledServiceDemoActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PollReceiver.scheduleAlarms(this);
Toast.makeText(this, R.string.alarms_scheduled, Toast.LENGTH_LONG)
.show();
finish();
}
}
//class PollReceiver
public class PollReceiver extends BroadcastReceiver {
private static final int PERIOD=30000; // 30sec
private static final int INITIAL_DELAY=5000; // 5 seconds
#Override
public void onReceive(Context ctxt, Intent i) {
if (i.getAction() == null) {
WakefulIntentService.sendWakefulWork(ctxt, ScheduledService.class);
}
else {
scheduleAlarms(ctxt);
}
}
static void scheduleAlarms(Context ctxt) {
AlarmManager mgr=
(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(ctxt, PollReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime() + INITIAL_DELAY,
PERIOD, pi);
}
}
//class ScheduledService
public class ScheduledService extends WakefulIntentService {
public ScheduledService() {
super("ScheduledService");
}
#Override
protected void doWakefulWork(Intent intent) {
Log.d(getClass().getSimpleName(), "I ran!"); // will run after 30 sec
}
}
//mainfest
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name="ScheduledServiceDemoActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="PollReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name="ScheduledService">
</service>
</application>
I have some code set up for the Status Notification, nothing is wrong with the code according to eclipse. However the code wont run when I start it up. Well It bypasses the notification and goes straight to the main activity. Why is my code skipping the notifications?
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class NoteMe extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int notifID = getIntent().getExtras().getInt("NotifID");
Intent i = new Intent("com.example.something.MainActivity");
PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher,
"STOP LOOK LISTEN!", System.currentTimeMillis());
CharSequence message = "This is your alert, courtesy of the AlarmManager";
notif.setLatestEventInfo(this, message, message, detailsIntent);
notif.vibrate = new long[] { 100, 250, 100, 500 };
nm.notify(notifID, notif);
finish();
}
}
Followed by Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.something"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NoteMe" >
<intent-filter>
<action android:name="com.example.something.NoteMe" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And logcat!
--Actually there is nothing in logcat (Strange)
In the manifest, if you remove the <action android:name="android.intent.action.MAIN" /> from the intent filter of your MainActivity and put it in the intent filter of your NoteMe activity, that activity should run on launch instead of the other. You just need to launch MainActivity from your NoteMe activity, right before you call finish().
Your onCreate() method is missing setContentView(). This should be called after super.onCreate(savedInstanceState). This tells Android which XML layout you would like to associate with the Activity.