Alarm Manager does not work in background - java

I used alarm manager in my program,but for the first time I run it, It just worked when my program was open and when it was close it did not work in the background, but in the second time it worked in the background too.
MainActivity :
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
Intent intent;
PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
intent = new Intent(MainActivity.this, AlertReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+60000, pendingIntent);
}
}
AlertReceiver :
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Log", "onReceiver!!");
}
}
AndroidManifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.yadame">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity" android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlertReceiver"/>
</application>

Related

Alarm manager execute method

This is part of a code that I combine together from google and youtube, to execute "turnOnLed" when the alarm go off. For some reason it doesnt execute and I don't know how to make it work. Can someone help? I've Google it, but no succes.
Thank you.
private void startAlarm(Calendar c)
{
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (c.before(Calendar.getInstance()));
{
c.add(Calendar.DATE, 1);
}
Intent intent = new Intent(this, Control.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), pendingIntent);
}
public class Control extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
turnOnLed(); //method to turn on
}
}
And the Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="art.control4.IntroScreen"
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="art.control4.DeviceList"
android:label="#string/app_name">
</activity>
<activity
android:name="art.control4.ledControl"
android:label="#string/app_name">
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<receiver android:name=".ledControl$Control"
android:process=":remote"/>

scheduling alarmmanager not working on reboot my phone

It's toasting until I didn't restart my phone but after restarting broadcastreceiver2 doesn't receive and nothing happens.
I followed http://stacktips.com/tutorials/android/how-to-start-an-application-at-device-bootup-in-android and many other but nothing happens.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.k2.alarmmanagerdemo">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.k2.alarmmanagerdemo.MyBroadcastReceiver"
android:enabled="true">
</receiver>
<receiver android:name="com.k2.alarmmanagerdemo.BroadCastRecevier2"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java
public class MainActivity extends Activity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAlert();
}
});
}
public void startAlert() {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),
1000 * 5,pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm...." + System.currentTimeMillis(), Toast.LENGTH_SHORT).show();
Log.i("Alarm.", "alarm called" + System.currentTimeMillis());
}
}
BroadCastRecevier2.java
public class BroadCastRecevier2 extends BroadcastReceiver {
MainActivity activity = new MainActivity();
#Override
public void onReceive(Context context, Intent intent) {
activity.startAlert();
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent i = new Intent();
i.setClassName("com.k2.alarmmanagerdemo",
"com.k2.alarmmanagerdemo.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Toast.makeText(context, "BOOT", Toast.LENGTH_SHORT).show();
Log.i("myboot","boot compleated inside");
}
}
}
Remove this line from your <receiver> declaration for BroadcastReceiver2:
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
That line in the <receiver> tag is telling the system that only packages which have this permission are allowed to send your class the Intent. Its' likely confusing the system and preventing the Intent from being sent to your receiver.
You may also find this article helpful when learning about alarms and the boot complete receiver.

android broadcast receiver and service to get notification

hi i have used the following code in receiver class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("the time is right","yay!");
Intent i = new Intent(context, AlarmServie.class);
context.startService(i);
}
}
here i the code which i used in service class
public class AlarmServie extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e("onStart:","came" );
/* NotificationManager notifyman = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent main_activity = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent o = PendingIntent.getActivity(this, 0, main_activity, 0);
/*Notification noti = new Notification.Builder(this)
.setContentTitle("Reminder to Pill")
.setContentText("Click for info")
.setAutoCancel(true)
.setContentIntent(o)
.build();*/
NotificationManager nm = (NotificationManager) this.getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
Intent in = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pending = PendingIntent.getactivity(this, 0, in, 0);
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(AlarmServie.this);
mBuilder.setContentTitle("Pill Reminder");
mBuilder.setContentText("CLick here to View");
//mBuilder.setSound(sound);
TaskStackBuilder ts=TaskStackBuilder.create(this);
ts.addParentStack(MainActivity.class);
nm.notify(9999,mBuilder.build());
}}
i created this code to get notification.when i run the app the receiver class is triggered but it is not moving to service class to invoke notification.can anyone say whats wrong in the code or say me how to get notification using broadcast receiver and service in a detailed tutorial
here is the manifest file and say me where to put the code exactly
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
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>
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
please explain in detail what the problem in code.please tell exactly where to put the service tag
You must declare service in manifest also.
Intent to start service is sent to system then system tries to find right application component to handle this particular intent using information passed in manifest files.
It should be enough, but I'm not sure if you put service in the default package.
<application>
....
<service android:name=".AlarmServie"/>
....
</application>
Your Manifest should look something like this
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
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>
//here is your service
<service android:name=".AlarmServie" android:enabled="true"/>
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>

Push notification not working - Android

I am trying to send a push notification on a button click event. But don't know why, but it is not working. It doesn't show any error though. Can anybody find any error in the code?
public class Verify extends AppCompatActivity {
public static String TAG=Verify.class.getSimpleName();
NotificationCompat.Builder notification;
private static final int uniqueID=12345;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify);
notification=new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
}
public void onVerify(View v)
{
this.defineNotification();
//other code follows
}
public void defineNotification()
{
notification.setContentTitle("Successfully Signed Up");
notification.setContentText("Hi, you just Signed Up as a Vendor");
notification.setWhen(System.currentTimeMillis());
Intent intent=new Intent(this,OtherActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(uniqueID,notification.build());
//Log.i(TAG, "coming here in notification");
}
Here is the Android Manifest Code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sachinparashar.xyz">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SignUp"
android:label="SignUp"
android:theme="#style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Verify"
android:label="#string/title_activity_verify"
android:theme="#style/AppTheme"/>
<activity
android:name=".VendorDetails"
android:label="Vendor Details"
android:theme="#style/AppTheme"/>
<activity
android:name=".OrderTypes"
android:label="Orders"
android:theme="#style/AppTheme" />
</application>
change this :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
with this :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Insted of
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
use
PendingIntent pendingIntent=PendingIntent.getActivity(Verify.this,0,intent,0);

getting action of buttons on receive() method which is a method of broadcast receiver but its show a null

I am getting action of buttons by getaction on receive() method which is a method of broadcast receiver but its sho a null plz tell me what I do in this to get a value which is not null
+my notify method is following
private void Notify(String notificationTitle, String notificationMessage)
{
String ns=Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager=(NotificationManager)getSystemService(ns);
#SuppressWarnings("deprecation")
Notification notification=new Notification(R.drawable.bg,"Time",System.currentTimeMillis());
RemoteViews notificationView=new RemoteViews(getPackageName(),R.layout.main);
Intent notificationIntent=new Intent(this,PlayerAudioActivity.class);
PendingIntent pendingNotificationIntent=PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentView=notificationView;
notification.flags|=Notification.FLAG_NO_CLEAR;
//supposed button call intent
Intent switchIntent=new Intent(this,MyReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.btnPrevious, pendingIntent);
notificationManager.notify(1, notification);
}
-my broadcastreceiver class is following
public class MyReceiver
extends BroadcastReceiver {
private static final String TAG = "waaaawoooooooooOOOOOOjnjkhdfku";
public void onReceive(Context context, Intent intent) {
final String action=intent.getAction();
// if(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)){
Log.d(TAG, ""+action);
// Bundle extras=intent.getExtras();
// String ieString=extras.getString("Locale");
// try{
// final int Appwidgid=extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
// if(Appwidgid!=AppWidgetManager.INVALID_APPWIDGET_ID)
// {
// this.onDeleted(context, new int[] { Appwidgid });
// }
// else {
// onReceive(context, intent);
// }
// context.startService(new Intent(context,PlayerAudioActivity.class));
// Log.i(TAG,"Starting Service ConnectivityListener");
// }catch(Exception e){
// Log.e(TAG,e.toString());
// }
// }
}
private void onDeleted(Context context, int[] is) {
// TODO Auto-generated method stub
}
}
+manifest file is following
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Audio.audioplayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/><action
android:name="android.net.conn.CONNECTIVITY_CHANGE"/><action
android:name="android.net.conn.DATA_ACTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<activity
android:name="com.Audio.audioplayer.PlayerAudioActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<data android:scheme="tel" />
</intent-filter>
</activity>
<activity
android:name=".PlayListActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.Audio.audioplayer.PlayerAudioActivity" />
</activity>
</application>
</manifest>
As adamp mentioned in this post you cannot access an action on an intent that you've not set:
{...}
//supposed button call intent
Intent switchIntent=new Intent(this, MyReceiver.class);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, switchIntent, 0);
{...}
You have to add your designated action to that intent by using setAction().
Something like this:
{...}
Intent switchIntent=new Intent(this, MyReceiver.class);
switchIntent.setAction("YOUR_ACTION");
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0, switchIntent, 0);
{...}
ps: just pseudo code

Categories

Resources