Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start application at particular time

I want to add this facility into my app in which user can set application start time and at that time application get started. How can i use broadcast receiver to open my app at user specific time. I am not sure this is possible in android or not? if you have any idea than please share. Here is main activity code

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv1=(ListView)findViewById(R.id.listView);
    final ImageView splashImage = (ImageView) findViewById(R.id.imageView1);
    splashImage.setBackgroundResource(R.layout.splash);
    AnimationDrawable  splashAnimation = (AnimationDrawable) splashImage.getBackground();
    splashImage.onWindowFocusChanged(true);
    splashAnimation.start();    
    AlarmManager am = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
    Date futureDate = new Date(new Date().getTime() + 86400000);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE, 1);// start app in 1 min again
    futureDate.setHours(0);
    futureDate.setMinutes(0);
    futureDate.setSeconds(20);
    Intent intent = new Intent(getBaseContext(), MyAppReciever.class);
    PendingIntent sender = PendingIntent.getBroadcast(getBaseContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis() , sender);}}

and this is reciver class code

class MyAppReciever extends BroadcastReceiver{ public void onReceive(Context context,Intent intent) {
startActivity(new Intent(context, Main_Activity.class));
}private void startActivity(Intent intent) {
// TODO Auto-generated method stub}}

I have added this line to manifest

<receiver  android:process=":remote" android:name="MyAppReciever"></receiver>

now my broadcast trigger but i got this error

10-02 17:56:27.735: E/AndroidRuntime(9020): java.lang.RuntimeException: Unable to instantiate receiver com.example.testgui.MyAppReciever: java.lang.IllegalAccessException: access to class not allowed

Thank you

like image 260
Swap-IOS-Android Avatar asked Oct 02 '12 14:10

Swap-IOS-Android


2 Answers

As @Paul D'Ambra said, you can do it with AlarmManager.

Example:

First you need to set the alarm:

AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE);

Date futureDate = new Date(new Date().getTime() + 86400000);


futureDate.setHours(8);
futureDate.setMinutes(0);
futureDate.setSeconds(0);


Intent intent = new Intent(con, MyAppReciever.class);

PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);

Next You need to create a reciever with some code to execute(ie- starting your app):

public class MyAppReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    startActivity(new Intent(context, MyApp.class));
    }
}
like image 97
Milos Cuculovic Avatar answered Nov 14 '22 22:11

Milos Cuculovic


Am just about to walk out of the office so no doubt someone can add a more detailed answer but you need to use AlarmManager

The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.

like image 24
Paul D'Ambra Avatar answered Nov 14 '22 21:11

Paul D'Ambra