Start the service after specific time android - android

i have made a service in my project and i want to start the start after every 2mins. i am using the folowing code but its not working properly.
public class ScheduleSync extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent)
{
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, StartMyServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), 60000*2, pending);
}
}
public class StartMyServiceReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "receive broadcast", Toast.LENGTH_LONG).show();
Log.d("StartMyServiceReceiver", "receive broadcast");
Intent service = new Intent(context, myservice.class);
context.startService(service);
}
}
kindly help me regarding this issue. when i see the taskmanager myservice is running at backend.

Why using AlarmManager?
Just use Thread.sleep(xxxx):
public class ScheduleSync extends BroadcastReceiver{
private Context context;
private void startOtherService(){
Intent i = new Intent(context, StartMyServiceReceiver.class);
context.startService(i);
}
#Override
public void onReceive(Context context, Intent intent)
{
this.context = context;
new Thread(new Runnable(){
public void run(){
try{
Thread.sleep(2*60*1000);
startOtherService();
}catch(InterruptedException e){
e.printStackTrace();
}
}
}).start();
}
}

Related

Using AlarmManager to activate and deactivate silence mode

In my program, I am getting 2 dates from user. First date is for activating silence mode and the second date is deactivating it. For handling this issue, I have tried to use 2 different AlarmManager and 2 different BroadcastReceiver but I couldn't achieve it.
I can activate silence mode at the first date but I couldn't deactivate it. This a part of my code:
public class AddEvent extends AppCompatActivity {
private static PendingIntent silenceActivatorPendingIntent;
private static PendingIntent silenceDeactivatorPendingIntent;
private static AlarmManager manager1;
private static AlarmManager manager2;
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent silenceActivatorIntent = new Intent(this, SilenceModeActivator.class);
Intent silenceDeactivatorIntent = new Intent(this, SilenceModeDeactivator.class);
silenceActivatorPendingIntent = PendingIntent.getBroadcast(this, 0, silenceActivatorIntent, 0);
silenceDeactivatorPendingIntent = PendingIntent.getBroadcast(this, 0, silenceDeactivatorIntent, 0);
manager2 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
manager1 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
}
public void addEvent(View view) {
GregorianCalendar targetStartDate = new GregorianCalendar(startYear, startMonth, startDay, startHour, startMinute, 0);
GregorianCalendar targetEndDate = new GregorianCalendar(endYear, endMonth, endDay, endHour, endMinute, 0);
manager1.set(AlarmManager.RTC_WAKEUP, targetStartDate.getTimeInMillis(), silenceActivatorPendingIntent);
manager2.set(AlarmManager.RTC_WAKEUP, targetEndDate.getTimeInMillis(), silenceDeactivatorPendingIntent);
}
public static void stopAlarm1() {
manager1.cancel(silenceActivatorPendingIntent);
}
public static void stopAlarm2() {
manager2.cancel(silenceDeactivatorPendingIntent);
}
}
addEvent is my button clicker method.
Silence Mode Activator:
public class SilenceModeActivator extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
activateSilentMode(context);
AddEvent.stopAlarm1();
}
public void activateSilentMode(Context context) {
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the notification policy access has been granted for the app.
if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
context.startActivity(intent);
}
mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
}
}
Silence Mode Deactivator:
public class SilenceModeDeactivator extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
deactivateSilentMode(context);
AddEvent.stopAlarm2();
}
public void deactivateSilentMode(Context context) {
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
// Check if the notification policy access has been granted for the app.
if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
context.startActivity(intent);
}
mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
}
}
Do you have any idea about how I can fix this?
Thanks in advance.

SendBroadcast() doesn't work in on receive of Broadcast

I have the main activity, that execute a service. In this service, I init a BroadcastReceiver (alarm).
Then I need send briadcast from the onReceive method of alarm broadcst, but doesn't work. But if I execute sendBroadcast() of other method in alarm, work perfectly.
See de code for explain me:
Activity (init service and get broadcastReceiver)
private final BroadcastReceiver abcd = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Intent in = getIntent();
//finish();
Log.d("sdasd", "onReceive: BROADCAST RECIBIDO!!!");
}
};
Service
alarm alarm = new alarm();
public void onCreate()
{
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
alarm.setAlarm(this, tiempo);
return START_STICKY;
}
Alarm.class
#Override
public void onReceive(Context context, Intent intent)
{
context.sendBroadcast(new Intent("xyz"));
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
}
public void setAlarm(Context context, int tiempo)
{
Log.e("TAG", "setAlarm: ");
context.sendBroadcast(new Intent("xyz"));
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(), tiempo, pi); // Millisec * Second * Minute
}
In alarm.class, the sendBroadcast in serAlarm() is executed, but the sendBroadcast in onreceive, doesn't executed. And the toast work perfectly.
Why?
I guess, you need to regist BroadcastReceiver in Service.class.
You can regist BroadcastReceiver in 2 way;
AndroidManifest.xml use
<receiver android:name=".TestReceiver">
<intent-filter>
<action android:name="xyz"/>
</intent-filter>
</receiver>
Sourcecode :
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("xyz");
registerReceiver(abcd, intentFilter);
private final BroadcastReceiver abcd = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Intent in = getIntent();
//finish();
Log.d("sdasd", "onReceive: BROADCAST RECIBIDO!!!");
}
};
======
".TestReceiver" example: It is Java Class File
public class TestReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
}}

AlarmManager with Inner Class BroadcastReceiver

I am new to android. I was playing with the AlarmManager and had successufully go a piece of code running with the BroadcastReceiver as a separate class.
I am now trying to put the BroadcastReceiver as inner class but have no luck on firing the BroadcastReceiver. I had no idea what might have gone wrong after hours looking at the code...
Here is my code:
public class InnerService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(InnerBroadcastReceiver.class.toString());
Log.d("InnerService","InnerService starts!");
Log.d("InnerService","class : "+InnerBroadcastReceiver.class.toString());
this.registerReceiver(new InnerBroadcastReceiver(), filter);
scheduleTestAlarmReceiver(this);
}
public static void scheduleTestAlarmReceiver(Context context) {
Log.d("scheduleTestAlarmReceiver", "scheduleTestAlarmReceiver start");
Intent receiverIntent = new Intent(context, InnerBroadcastReceiver.class);
receiverIntent.setAction("com.example.alarmmanagertest.InnerService$InnerBroadcastReceiver");
PendingIntent sender = PendingIntent.getBroadcast(context, 123456789,
receiverIntent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 1000, sender);
Log.d("scheduleTestAlarmReceiver", "scheduleTestAlarmReceiver complete");
}
private class InnerBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("InnerBroadcastReceiver","InnerBroadcastReceiver ALARM Manager fires success!");
}
}
}
It looks like the AlarmManager tried to fire the BroadcastReceiver every second but failed
Logcat:
V/AlarmManager(2439): waitForAlarm result :4
V/AlarmManager(2439): trigger ELAPSED_REALTIME_WAKEUP or RTC_WAKEUP
UPDATE
I have tried to change the code for creating intent in onCreate() and scheduleTestAlarmReceiver() to intent = new intent("action_string") and it works. It seems that intent.setAction() is not working.
What will be the pros and cons for creating intent with and without context (Intent(Context packageContext, Class<?> cls) and Intent(String action))?
But I would still like to know why the above code failed. Can anyone explain?
True, it works!
Small changes in my code. Apk with AsyncTask (downloading file from web and parsing it). "OneMeeting" is my class from project.
public class MainActivity extends AppCompatActivity {
public static final String INPUT_FILE_URL = "https://.../";
private RecyclerView recyclerView;
private String getFilesDir;
private ArrayList<OneMeeting> meetingsArr = new ArrayList<>();
private BroadcastReceiver receiver;
private RefreshFile refreshFile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
getFilesDir = getFilesDir().getAbsolutePath();
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
meetingsArr.add(new OneMeeting("Launching..." , "", "", ""));
recyclerView.setAdapter(new MyAdapter(meetingsArr, recyclerView));
Intent alarmIntent = new Intent("commaciejprogramuje.facebook.conferenceapplication.MainActivity$RefreshFile");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 111, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 5, pendingIntent);
}
#Override
protected void onResume() {
super.onResume();
refreshFile = new RefreshFile();
IntentFilter filter = new IntentFilter("commaciejprogramuje.facebook.conferenceapplication.MainActivity$RefreshFile");
this.registerReceiver(refreshFile, filter);
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(refreshFile);
}
private class RefreshFile extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Refresh", Toast.LENGTH_LONG).show();
ParsePage refreshParsingPage = new ParsePage(new ParsePage.OnTaskCompletedListener() {
#Override
public void onTaskCompletedListener(ArrayList<OneMeeting> parsingResultArr) {
meetingsArr = parsingResultArr;
recyclerView.setAdapter(new MyAdapter(meetingsArr, recyclerView));
}
});
refreshParsingPage.execute(getFilesDir);
}
}
}

AlarmManager to start an activity with onBootReceiver

I developed an app to start an activity at everyday 11pm. I also added a boot receiver on it. the output of this app gives nothing but a blank screen. Kindly someone help me.
This is my first class OnBootService
public class boot extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent bootintent) {
Intent mServiceIntent = new Intent();
mServiceIntent.setAction("com.thenga.nilavilak.timer_test001.alarm");
context.startService(mServiceIntent);
}
}
This is my alarm class
public class alarm extends boot {
public void onReceiveboot(final Context context) {
Calendar vtime = Calendar.getInstance();
vtime.set(Calendar.HOUR_OF_DAY,23);
vtime.set(Calendar.MINUTE,0);
vtime.set(Calendar.SECOND,0);
;
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, sasi.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, vtime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
}
public IBinder onBind(Intent arg0) {
return null;
}
}
This is my third class the want to run at 11 pm
public class sasi extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getApplicationContext(),"Output received",Toast.LENGTH_SHORT).show();
}
}

Android AlarmManager

Ok, I've tried two examples of AlarmManager- one from the commonsware website, and one from the manning website.
The code I am currently working with is from the manning website : [http://unlocking-android.googlecode.com/svn/chapter8/trunk/SimpleAlarm/][1]
There are two classes, AlarmReceiver and GenerateAlarm. Anyone have any idea why the toast will not display in the emulator? I was thinking that it was because I am located in the Eastern Time Zone and it uses UTC, but I have fiddled with different things and none of them seem to work.
public class GenerateAlarm extends Activity {
Toast mToast;
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.set_alarm_button);
button.setOnClickListener(this.mOneShotListener);
}
private OnClickListener mOneShotListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(GenerateAlarm.this, AlarmReceiver.class);
PendingIntent appIntent = PendingIntent.getBroadcast(GenerateAlarm.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// calendar.add(Calendar.MINUTE, 1);
calendar.add(Calendar.SECOND, 10);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), appIntent);
if (GenerateAlarm.this.mToast != null) {
GenerateAlarm.this.mToast.cancel();
}
GenerateAlarm.this.mToast = Toast.makeText(GenerateAlarm.this, R.string.alarm_message, Toast.LENGTH_LONG);
//GenerateAlarm.this.mToast.show();
}
};
}
public class AlarmReceiver extends BroadcastReceiver {
public void onReceiveIntent(Context context, Intent intent) {
Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
}
#Override
public void onReceive(Context context, Intent intent) {
}
}
You have to add your toast in the onReceived method where you should add the handling of the intent received.
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
}
onReceiveIntent is not a method of broadcastreceiver
public abstract void onReceive
(Context context, Intent intent)
Since: API Level 1 This method is
called when the BroadcastReceiver is
receiving an Intent broadcast.

Categories

Resources