0

i am working on a android app where it is need to start a activity from another activity using broadcast receiver if my application is in background then my activity goes to pause state then how i can start another activity please help me why it is happening code is

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent vidIn = new Intent();
    vidIn.setClass(this, Activity1.class);
    vidIn.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(vidIn);
    }
}

1 Answers1

0

BroadcastReceiver has to be registered in AndroidManifest.xml (to work standalone)

<receiver android:name="com.example.MyBroadcastReceiver"/>

Intent which will fire this will have a form:

Intent startActivityIntent = new Intent(context, NewActivity.class);
startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startActivityIntent);
robotoaster
  • 3,082
  • 1
  • 24
  • 23
  • Actually i am receiving broadcast from screen On and OFF event , which never work if we define them in menifest file .registerReceiver(trigerReceiverClass, new IntentFilter(Intent.ACTION_SCREEN_ON)); registerReceiver(trigerReceiverClass, new IntentFilter(Intent.ACTION_SCREEN_OFF)); – Janeshwar Yashpal Jun 15 '13 at 08:43
  • Why a pause activity can not start a new activity where it is not destroyed . – Janeshwar Yashpal Jun 15 '13 at 08:54
  • in theory receivers should be unregistered if activity is paused. http://stackoverflow.com/questions/4619374/when-to-unregister-broadcast-receiver. That's why broadcast receiver in manifest is your friend. It will wake up even if entire Application is dead. – robotoaster Jun 15 '13 at 17:48
  • Thanx , gentleman one more query if i keep my activity in background using the method moveTaskToBack(true) for a long time and when i starts again then it calls onCreate() method . Sir what should i do to keep my activity running for a long time in background. code is – Janeshwar Yashpal Jun 16 '13 at 15:29
  • I would implement it as a service and run it in a foreground using startForeground(). Background tasks are considered low priority and Dalvik can kill them anytime. Read this answer http://stackoverflow.com/questions/3856767/android-keeping-a-background-service-alive-preventing-process-death – robotoaster Jun 17 '13 at 07:57