I start work with a broadcast receiver, but why does my other activity begin?

There is MainActivityand in my application AlarmActivity. MainActivityUsed to set the alarm. AlarmActivityused to display the alarm. In the middle, I have a class that implements BroadcastReceiverto get the pending intent that was planned for MainActivity. The question I have is double.

1) MainActivityis created when I expect it to AlarmActivitybe created using the onReceive method shown below.

2) When AlarmActivitycreated, it is immediately destroyed. See My Logs below.

Please note that this ONLY happens when I turn off the screen. If I destroy MainActivityand turn on the screen until the alarm goes off, everything will be fine. If I allow MainActivityto live and hold the screen, it is AlarmActivitydisplayed at the top MainActivity, as expected. Otherwise, if the screen is off ... AlarmActivity is displayed for a second, then it disappears, and I see MainActivity on the screen. What gives?

This is where I plan the alarm. Not that the callContext variable is the context of my MainActivity class.

 Intent intentAlarm = new Intent(this.callingContext, AlarmActivity.class);

        //Get the Alarm Service
        AlarmManager alarmManager = (AlarmManager) callingContext.getSystemService(Context.ALARM_SERVICE);

     //user our intent and give it a time to broadcast at
          alarmManager.set(AlarmManager.RTC_WAKEUP,alarmTime, PendingIntent.getBroadcast(this.callingContext, 1, intentAlarm, PendingIntent.FLAG_CANCEL_CURRENT));


public class AlarmReceiver extends BroadcastReceiver { 

    final String TAG=this.toString();

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Log.i(TAG,"onReceive");

        //start a new activity, an alarm has gone off

        //See that I've tried using the application context, but this did not help.
        //Context context=arg0.getApplicationContext();
        Intent intent=new Intent(arg0,AlarmActivity.class);

        //This flag is required for starting an activity outside of an activity.
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        intent.putExtra("ACTIVITY_STARTED_FROM_BROADCAST_RECEIVER",true);
        arg0.startActivity(intent);
    }
}

Lifecycle methods of anxiety activity.

protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG,"onCreate");
        if(savedInstanceState==null) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_alarm);


        }
    }

    public static void acquireScreenCpuWakeLock(Context context) {
        if (sCpuWakeLock != null) {
            return;
        }
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        sCpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "TYLER");
        sCpuWakeLock.acquire();
    }

    public static void releaseCpuLock() {
        if (sCpuWakeLock != null) {
            sCpuWakeLock.release();
            sCpuWakeLock = null;
        }
    }
    @Override
    protected void onResume() {
        Log.i(TAG,"onResume");
        super.onResume();

    //  getFragmentManager().beginTransaction().add(R.id.alarm_frame, new AlarmFragment()).commit();
    }

    @Override
    protected void onStart() {
        Log.i(TAG,"onStart");
        super.onStart();
        final Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
        acquireScreenCpuWakeLock(this);
        alarmFragment=new AlarmFragment();
        alarmFragment.setArguments(getIntent().getExtras());
        getFragmentManager().beginTransaction().add(R.id.alarm_frame, alarmFragment).commit();

    }

    @Override
    protected void onPause(){
        Log.i(TAG,"onPause");
        super.onPause();
        releaseCpuLock();
        getFragmentManager().beginTransaction().remove(alarmFragment);
        Log.d("pfaff","wakelock released, alarm activity paused");

    }

   @Override
    protected  void onDestroy(){
       Log.i(TAG,"onDestroy");
       super.onDestroy();
     //  wakeLock.release();
        Log.d("pfaff","alarm activity destroyed");
   }

}

Lifecycle Methods Alarm Framgnet.

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_alarm, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity){
        Log.i(TAG,"onAttach");
        super.onAttach(activity);
        this.activity=activity;//camera

    }
    @Override
    public void onStart() {
        Log.i(TAG, "onStart");
        super.onStart();
    }

    @Override
    public void onResume() {
       Log.i(TAG,"onResume");
        super.onResume();
        final AlarmFragment thisFragment = this;
}

@Override
    public void onPause() {
        Log.i(this.toString(),"onPause");
        super.onPause();
        getActivity().onBackPressed();

    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Log.i(this.toString(),"onDestroy");

        super.onDestroy();
       // tearDownMedia();
        scanMyMedia();
        releaseCameraAndPreview();
        if(timeReceiverIsRegistered) {
            getActivity().unregisterReceiver(_broadcastReceiver);
        }
    }


here is logcat:

   11-27 21:56:46.036  29370-29370/com.example.videoalarmt I/com.example.videoalarmt.AlarmReceiver@41eb2158﹕ onReceive
    11-27 21:56:46.066  29370-29370/com.example.videoalarmt I/com.example.videoalarmt.MainActivity@41eaaf08﹕ onCreate
    11-27 21:56:46.066  29370-29370/com.example.videoalarmt I/timePickerFragment{41ea1e58}﹕ onCreateView
    11-27 21:56:46.096  29370-29370/com.example.videoalarmt E/TextView﹕ Saved cursor position 2/2 out of range for (restored) text
    11-27 21:56:46.096  29370-29370/com.example.videoalarmt E/TextView﹕ Saved cursor position 2/2 out of range for (restored) text
    11-27 21:56:46.136  29370-29370/com.example.videoalarmt I/com.example.videoalarmt.AlarmActivity@41ed7bf8﹕ onCreate
    11-27 21:56:46.136  29370-29370/com.example.videoalarmt I/com.example.videoalarmt.AlarmActivity@41ed7bf8﹕ onStart
    11-27 21:56:46.136  29370-29370/com.example.videoalarmt I/com.example.videoalarmt.AlarmActivity@41ed7bf8﹕ onResume
    11-27 21:56:46.136  29370-29370/com.example.videoalarmt I/AlarmFragment{41edaa40}﹕ onAttach
    11-27 21:56:46.136  29370-29370/com.example.videoalarmt I/AlarmFragment{41edaa40}﹕ onViewCreated
    11-27 21:56:46.136  29370-29370/com.example.videoalarmt I/AlarmFragment{41edaa40}﹕ onStart
    11-27 21:56:46.136  29370-29370/com.example.videoalarmt I/AlarmFragment{41edaa40}﹕ onResume
    11-27 21:56:46.136  29370-29370/com.example.videoalarmt I/AlarmFragment{41edaa40}﹕ was started from broadcast receiver
    11-27 21:56:46.206  29370-29370/com.example.videoalarmt I/AlarmFragment{41edaa40 #0 id=0x7f0a0024}﹕ onPause
    11-27 21:56:46.206  29370-29370/com.example.videoalarmt I/com.example.videoalarmt.AlarmActivity@41ed7bf8﹕ onPause
    11-27 21:56:46.787  29370-29370/com.example.videoalarmt I/AlarmFragment{41edaa40 #0 id=0x7f0a0024}﹕ Putting media recorder in PREPARED state from DATASOURCECONFIGURED state
    11-27 21:56:46.787  29370-29370/com.example.videoalarmt I/MediaRecorderJNI﹕ prepare: surface=0x76608e58
    11-27 21:56:47.137  29370-29370/com.example.videoalarmt E/MediaPlayer﹕ Should have subtitle controller already set
    11-27 21:56:47.137  29370-29370/com.example.videoalarmt I/Choreographer﹕ Skipped 53 frames!  The application may be doing too much work on its main thread.
    11-27 21:56:47.237  29370-29370/com.example.videoalarmt I/com.example.videoalarmt.AlarmActivity@41ed7bf8﹕ onStop
    11-27 21:56:47.598  29370-29370/com.example.videoalarmt I/AlarmFragment{41edaa40 #0 id=0x7f0a0024}﹕ onDestroy
    11-27 21:56:47.598  29370-29370/com.example.videoalarmt I/com.example.videoalarmt.AlarmActivity@41ed7bf8﹕ onDestroy
    11-27 21:56:47.658  29370-29370/com.example.videoalarmt W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
    11-27 21:57:02.243  29370-29370/com.example.videoalarmt I/com.example.videoalarmt.MainActivity@41eaaf08﹕ onDestroy


Here is my manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.videoalarmt"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="21" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.front" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Holo.Light.NoActionBar"
        >

        <meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

           <receiver android:name="com.example.videoalarmt.AlarmReceiver"/>
        <activity
            android:name="com.example.videoalarmt.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
          <activity
            android:name="com.example.videoalarmt.AlarmActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            >
        </activity>

            <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

    </application>



</manifest>
+4
6

, MainActivity. , , :

Intent intentAlarm = new Intent(this.callingContext, AlarmActivity.class);

Activity, getBroadCast(). :

Intent intentAlarm = new Intent(this.callingContext, AlarmReceiver.class);

, . , , mainactivity ?

+2

, onBackPressed .

11-27 21:56:46.206  29370-29370/com.example.videoalarmt I/AlarmFragment{41edaa40 #0 id=0x7f0a0024}﹕ onPause

AlarmFragment.onPause getActivity().onBackPressed(). , . AlarmFragment.onPause, , .

+1

onReceive() BroadcastReceiver 10 . . .

, cpu .

, cpu , 10 , .

, . .
. link1 link2

, .

0
intent intent = new Intent(context, WordService.class);  
context.startService(intent);
0

, . , , , . , , , .

, :

, , . , , , . , , , .

. -, . , SharedPreferances. . . .

<uses-permission android:name="android.permission.WAKE_LOCK" />

, - . .

go getActivity().finish() not getActivity().onBackPressed(), .

0

When you press the lock button, the application goes into pause mode. If you want the application to do something (or continue to do something), you need to handle this in the onPause () method of your main action.

-1
source

All Articles