Cannot start Service in API 23 - android

I am trying to experiment with services, and I found this example:
Alarm Manager Example.
I tried using the accepted answer, and the changes AndroidDev suggested, but I cant get it to work. Based on the fact that none of my Log messages appear, I think that the Service isn't called.
My onCreate method is this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent serviceIntent = new Intent(this, YourService.class);
startService(serviceIntent);
Vibrator v=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
the Service is this:
package habos.alarmtest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class YourService extends Service
{
Alarm alarm = new Alarm();
String tag="Babis service";
public void onCreate()
{
super.onCreate();
Log.i(tag, "oncreate");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(tag, "onstartcommand");
alarm.setAlarm(this);
return START_STICKY;
}
#Override
public void onStart(Intent intent, int startId)
{
alarm.setAlarm(this);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
}
The onStart method is deprecated, but onStartCommand should work.
the manifest is this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="habos.alarmtest">
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<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">
<receiver android:name=".Alarm" android:exported="true">
<intent-filter>
<action android:name="habos.START_ALARM" >
</action>
</intent-filter>
</receiver>
<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>
</application>
and the alarm is this
package habos.alarmtest;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;
import android.os.Vibrator;
public class Alarm extends BroadcastReceiver
{
String tag="Babis alarm";
#Override
public void onReceive(Context context, Intent intent)
{
Log.i(tag, "onreceive");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Put here YOUR code.
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
Vibrator v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
wl.release();
}
public void setAlarm(Context context)
{
Log.i(tag,"setalarm");
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent("mypackage.START_ALARM");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 30,pi); // Millisec * Second * Minute
}
public void cancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
added 2 more Logs, before and after the startService,the logcat is here:
https://www.dropbox.com/s/0ksdrfzyjb0qafe/logcat.txt?dl=0
(sorry I didn't paste it directly here, the code block didn't work for some reason)

Related

How can I start android application on device boot?

I have need to start android foreground service, and launch activity from that service on device boot. I have extensively searched web and stackoverflow and tried different suggestions but it is very strange that I can not make this functionality work.
I can not understand what I am doing wrong.
Below is the code from my project and the content of manifest file.
what I am doing wrong and how to solve it, the functionality to work on most android devices?
This is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kor.location.tracker">
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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">
<receiver android:name="kor.location.tracker.AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<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>
<service android:enabled="true"
android:name="kor.location.tracker.WorkerService"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
</application>
</manifest>
This is my Austostart.java:
package kor.location.tracker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
public class AutoStart extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent arg1)
{
try {
System.out.println("test1");
if (Intent.ACTION_BOOT_COMPLETED.equals(arg1.getAction())) {
System.out.println("test2");
WorkerService.enqueueWork(context, new Intent());
System.out.println("test3");
}
}catch(Exception ex) {
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
}
/*
Intent intent = new Intent(context, WorkerService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
Log.i("Autostart", "started");
*/
}
}
This is my service class WorkerService.java:
package kor.location.tracker;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.JobIntentService;
public class WorkerService extends JobIntentService
{
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, WorkerService.class, 104501, work);
}
/*
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
*/
#Override
protected void onHandleWork(#NonNull Intent intent) {
Intent intents = new Intent(getBaseContext(),MainActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
//Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
//Log.d(TAG, "onStart");
}
/*
#Override
public void onStart(Intent intent, int startid)
{
final LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(final Location location) {
//your code here
String kuku = location.getLatitude() + "=" + location.getLongitude();
Toast.makeText(WorkerService.this, kuku, Toast.LENGTH_LONG).show();
Log.d(TAG, kuku);
;
;
location.getAltitude();
location.getSpeed();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000,
1, mLocationListener);
}catch (SecurityException ex){
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
Log.d(TAG, ex.getMessage());
}
Intent intents = new Intent(getBaseContext(),MainActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
*/
}
And this is my activity that is not getting launched:
package kor.location.tracker;
import android.Manifest;
import android.content.pm.PackageManager;
import android.nfc.Tag;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Toast.makeText(getBaseContext(), "Hello........", Toast.LENGTH_LONG).show();
List<String> permissions = new ArrayList<String>();
if(getApplicationContext().checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED){
permissions.add(Manifest.permission.INTERNET);
}
if(getApplicationContext().checkCallingOrSelfPermission(Manifest.permission.RECEIVE_BOOT_COMPLETED) != PackageManager.PERMISSION_GRANTED){
permissions.add(Manifest.permission.RECEIVE_BOOT_COMPLETED);
}
if(getApplicationContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if(getApplicationContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if(getApplicationContext().checkCallingOrSelfPermission(Manifest.permission.FOREGROUND_SERVICE) != PackageManager.PERMISSION_GRANTED){
permissions.add(Manifest.permission.FOREGROUND_SERVICE);
}
if(permissions.size()>0) {
String[] arr = new String[permissions.size()];
permissions.toArray(arr);
//System.out.println();
ActivityCompat.requestPermissions(this, arr, 1);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
1- For Problem Starting Activity From Background
in API29 Android restricted, starting an activity from the background. The foreground service is also considered a background process. Your activity can be affected by this restriction if you test it in Android 10.
Android Q Restrictions: https://developer.android.com/guide/components/activities/background-starts
Possible solution:
https://stackoverflow.com/a/59421118/11982611
2- Some brands limits applications to start in the boot to boost start up time of the device. So applications need exclusive permission to start in the boot.
Possible Solution (Programmatically) :
https://stackoverflow.com/a/49167712/11982611
For Xiaomi, enabling Autostart from settings
https://dontkillmyapp.com/xiaomi
After I added the boot permissions in manifest file (see code below), BroadcastReceiver started to get boot completed event and the service is starting successfully. For the solution to work (as suggested by #Eren Tüfekçi) I had to enable auto start permission in phone settings for my application. If anyone has a solution, how to enable it programmatically, please let us know. Thank you.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kor.location.tracker">
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<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">
<receiver android:name="kor.location.tracker.AutoStart"
android:enabled="true"
android:exported="true">
<intent-filter android:directBootAware="true">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
<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>
<service android:enabled="true"
android:name="kor.location.tracker.WorkerService"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
</application>
</manifest>
Check this. In this way what you want to achieve should work. If you can not, logs should be followed to find out the problem.
public class AutoStart extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent arg1)
{
try {
Intent intent = new Intent(context, WorkerService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}catch(Exception ex) {
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
In Service
public class WorkerService extends Service {
public static final String CHANNEL_ID = "ForegroundServiceChannel";
public static final String NEW_CHANNEL_ID = "AndroidForegroundServiceChannel";
Notification notification;
#Override
public void onCreate() {
super.onCreate();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{createNotificationChannel(); //for Android Oreo above notification channel mandatory }
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
{// if Android 10 create a pending intent and a full screen notification
Intent fullScreenIntent = new Intent(this, "Your Activity".class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 2022,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT); // For the activity opening when notification cliced
notification= new NotificationCompat.Builder(this, NEW_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("Notification title")
.setContentText("Notification Text")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setFullScreenIntent(fullScreenPendingIntent, true)
.build();
startForeground(2, notification);
}
else
{
//if below Android 10 created a notification for foreground service because it is mandatory
Intent notificationIntent = new Intent(this, Your Activity.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0022,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentText("Foreground Service")
.setSmallIcon(R.drawable.ic_notification)
.setSound(null)
.setContentIntent(pendingNotificationIntent)
.build();
//for below Android 10 started activity
Intent i = new Intent(getApplicationContext(), Your Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(i);
}
startForeground(1, notification);
}
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "Foreground Service fault", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return START_NOT_STICKY;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NotificationChannel serviceChannel = new NotificationChannel(
NEW_CHANNEL_ID,
"Android Foreground Service Channel",
NotificationManager.IMPORTANCE_HIGH
);
serviceChannel.setSound(null,null);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
serviceChannel.setSound(null,null);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
I can still start the app on Boot. (Targeting API 29). By using broadcast receiver with intent ACTION_BOOT_COMPLETED.
The problem I faced after updating my android to version 9 on Honor brand of phone was introduction of advance app management for probably battery preservation which prevented my app from receiving the broadcast in the first place.
Go to Settings > Battery > App Launch > Go to your app and uncheck "Manage Automatically" > And make sure "Auto-launch", "Secondary launch", "Run in background" is checked and select "OK"
Reboot your phone and check if the app starts on boot or not. Hope this helps someone else.

Intent Service doesn't run from Broadcast Receiver

I followed this tutorial for Repeating Alarm Example in Android using AlarmManager: http://javatechig.com/android/repeat-alarm-example-in-android.
I want to start an alarm which will trigger an intent service every 10 seconds. But it doesn't work, if there is someone who can help me.
MainActivity code:
package subhi.com.broadcast2;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(MainActivity.this, MyService.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAt10();
}
});
}
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancel() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
public void startAt10() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 20;
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, pendingIntent);
}
}
The AlarmReciver code, which will start the IntentService:
package subhi.com.broadcast2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Created by subhi on 2/20/2016.
*/
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
//Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
Intent intent2=new Intent(context,MyService.class);
context.startService(intent);
}
}
The MyService code:
package subhi.com.broadcast2;
import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by subhi on 2/20/2016.
*/
public class MyService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* #param name Used to name the worker thread, important only for debugging.
*/
public MyService() {
super("My_Worker_Thread");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"Service Started....",Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,"Service Stopped....",Toast.LENGTH_LONG).show();
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getApplicationContext(),"Good",Toast.LENGTH_LONG).show();
synchronized (this){
int count=0;
while (count<10) {
try {
wait(3000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Finally the manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="subhi.com.broadcast2">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".MyService">
</service>
</application>
</manifest>
The broadcast receiver is having a BOOT_COMPLETED intent filter. Thus the receiver will only receive a broadcast on boot complete. Instead use your MyService intent service in the pending intent as follows
PendingIntent intent = PendingIntent.getService(context, 0, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);

Add intent to an alarm but not run it immediately

My scenario is that I want to run the receiver on a specific time that I set with the AlarmManager.
Here is some relevant code:
AlarmManager aManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, MyBroadcastReceiver.class);
aManager.set(AlarmManager.RTC, myTimeInMillis, PendingIntent.getBroadcast(context, 0, intent, 0));
I have tried to add the FLAG_RECEIVER_REGISTERED_ONLY-flag to the intent, and this had the desired effect by not running immediately when I created the intent but the AlarmManager didn't seem to be able to run the receiver when that flag was set.
Is it possible to add an intent that will run a broadcast-receiver to an AlarmManager in Android?
This is working code. It wakes CPU every 10 minutes until the phone turns off.
Add to Manifest.xml:
...
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
...
<receiver android:process=":remote" android:name="Alarm"></receiver>
...
Code:
package YourPackage;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.widget.Toast;
public class Alarm extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Put here YOUR code.
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
Set Alarm from Service:
package YourPackage;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class YourService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
alarm.SetAlarm(YourService.this);
return START_STICKY;
}
public void onStart(Context context,Intent intent, int startId)
{
alarm.SetAlarm(context);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
}
If you want set alarm repeating at phone boot time:
Add permission to Manifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
...
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
...
And create new class:
package YourPackage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoStart extends BroadcastReceiver
{
Alarm alarm = new Alarm();
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
alarm.SetAlarm(context);
}
}
}

AlarmManager wont fire pending intent

I try to use AlarmManager with BroadcastReceiver.
My problem is that I dont get the intent fire from the AlarmManager.
Im not getting any call from Log.i("test", "test")
Here is my code:
public void activityCheck()
{
Intent intent = new Intent(this.getApplicationContext(), com.example.workoutlog.UsageCheckService.class);
intent.putExtra("nofitication", "nofitication");
PendingIntent service = PendingIntent.getService(this, 0, intent, 0);
AlarmManager m = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
Log.i("Yo", "Yo");
m.setRepeating(AlarmManager.RTC, time.getTimeInMillis(), 1000*60, service);
}
My BroadcastReceiver class
public class UsageCheckService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("test", "test");
}
}
In my manifest
<receiver android:name="com.example.workoutlog.UsageCheckService" >
</receiver>
If the phone is in sleep state, you will have to wake it up.
Try this:
A small example:
This is working code. It wakes CPU every 10 minutes until the phone turns off.
Add to Manifest.xml:
...
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
...
<receiver android:process=":remote" android:name="Alarm"></receiver>
...
Code:
package YourPackage;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.widget.Toast;
public class Alarm extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Put here YOUR code.
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
Set Alarm from Service:
package YourPackage;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class YourService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
super.onCreate();
}
public void onStart(Context context,Intent intent, int startId)
{
alarm.SetAlarm(context);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
}
If you want set alarm repeating at phone boot time:
Add permission to Manifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
...
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
...
And create new class:
package YourPackage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoStart extends BroadcastReceiver
{
Alarm alarm = new Alarm();
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
alarm.SetAlarm(context);
}
}
}
If you device is in "sleep mode" (battery save), the Broadcast will not wake up it.
You must use WakefulBroadcastReceiver
https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

how to get id of broadcastreceiver in android

I want to get the id of broadcastriver.
Is there any way to find the id of the broadcastreciver?? I am making an alarm application and on saving the alarm, I send a broadcast using pending intent. Now if a user edit the alarm, I want to abort that specif broadcast and send a new broadcast of the new time?
How can I do this????
I am using this code for broadcast
Intent intent = new Intent(this, StartProfileBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, StartTimemill, pendingIntent);
and in manifest, I register my Broadcastreciver using this code
<receiver android:name="StartProfileBroadcastReceiver" >
</receiver>
you can find the project on my github by this project you can et lots of alarm and can catch cancaled alarms.
Alarm class is needed to hold to get specific data
Alarm.java
public class Alarm {
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public int getUniqueID() {
return uniqueID;
}
public void setUniqueID(int uniqueID) {
this.uniqueID = uniqueID;
}
private int state;
private int uniqueID;
}
AlarmHolder is singleton and holds the alarm in an arraylist
AlarmHolder.java
import java.util.ArrayList;
public class AlarmHolder{
private static AlarmHolder uniqueAlarmHolder;
private ArrayList<Alarm> lAlarms;
private AlarmHolder() {
lAlarms = new ArrayList<Alarm>();
}
public synchronized static AlarmHolder getInstance()
{
if (uniqueAlarmHolder == null) {
uniqueAlarmHolder = new AlarmHolder();
}
return uniqueAlarmHolder;
}
public void registerAlarm(int id) {
Alarm a = new Alarm();
a.setState(1);
a.setUniqueID(id);
lAlarms.add(a);
}
public void removeAlarm(int id,Alarm a) {
Alarm newAlarm = new Alarm();
a.setState(0);
a.setUniqueID(id);
lAlarms.remove(id);
lAlarms.add(newAlarm);
}
public void replaceList(ArrayList<Alarm> newList) {
lAlarms.clear();
lAlarms.addAll(newList);
}
public ArrayList<Alarm> getAlarms() {
return lAlarms;
}
public Alarm lastAlarmId() {
return lAlarms.get(lAlarms.size()-1);
}
}
MyBroadcastReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
AlarmHolder objAlarmHolder = AlarmHolder.getInstance();
#Override
public void onReceive(Context context, Intent intent) {
// with this key we can catch the alarm which we want
int key = intent.getIntExtra("key",0);
for (Alarm alarm : objAlarmHolder.getAlarms()) {
// if alarmstate is 1 and the key belogs that alarm device will vibrate
if (alarm.getState() == 1 & alarm.getUniqueID() == key ) {
Log.v("alarm", String.valueOf(String.valueOf(alarm.getUniqueID())));
Toast.makeText(context, "your alarm id : " + String.valueOf(alarm.getUniqueID()),Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
} else {
Log.v("alarm", "canceled alarm number : " + String.valueOf(alarm.getUniqueID()));
}
}
}
}
MainActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText text;
int uniqueInteger = 1;
AlarmHolder objAlarmHolder = AlarmHolder.getInstance();
ArrayList<Alarm> tempAlarmHolder = new ArrayList<Alarm>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// we should pass unique integer to method
startAlert(uniqueInteger);
uniqueInteger++;
}
});
}
public void startAlert(int rnd) {
int i = Integer.parseInt(text.getText().toString());
Bundle bundle = new Bundle();
bundle.putInt("key", uniqueInteger);
objAlarmHolder.registerAlarm(uniqueInteger);
int alarmCount = 0;
tempAlarmHolder.clear();
// in this loop, all old alarms' state will set as 0
for (Alarm alarm : objAlarmHolder.getAlarms()) {
if(alarmCount < objAlarmHolder.getAlarms().size()-1)
{
Alarm objAlarm = new Alarm();
objAlarm.setState(0);
objAlarm.setUniqueID(alarmCount);
tempAlarmHolder.add(objAlarm);
}
else
{
tempAlarmHolder.add(alarm);
}
alarmCount++;
}
objAlarmHolder.replaceList(tempAlarmHolder);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), getPendingIntent(bundle, uniqueInteger));
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}
private PendingIntent getPendingIntent(Bundle bundle, int rc) {
Intent intent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
// send alarm id to broatcast
intent.putExtras(bundle);
return PendingIntent.getBroadcast(this, rc, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
Manifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.VIBRATE" ></uses-permission>
<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>
<receiver android:name="MyBroadcastReceiver" >
</receiver>
</application>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
You can either:
Keep a reference to the PendingIntent object you register with the AlarmManager, and call PendingIntent.cancel to cancel it when you're registering a new alarm.
Use the flag FLAG_CANCEL_CURRENT when creating your PendingIntent.
Using both methods, the old alarm will be deleted when creating the new one..

Categories

Resources