首次启动时会调用onResume()和onPause()

时间:2019-05-31 08:06:30

标签: java android android-activity android-lifecycle

我正在进行一项活动,并在通过服务之一中的Intent启动该活动时调用了onCreate(),onPause()和onResume()。

我不太确定自己在哪里做错了,我已经删除了所有代码,只有基本的功能代码可用,并且我也删除了所有打开该特定活动的代码,但行为仍然相同。

我包括了我正在使用的最简单的代码

ActivityClass.java

public class ActivityClass extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("TEST : onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lockscreen);
        ButterKnife.bind(this);

    }

    @Override
    protected void onPause() {
        System.out.println("TEST : onPause");
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        System.out.println("TEST : onDestroy");
        super.onDestroy();
    }

}

服务等级

public class OverlayService extends Service {

    Context context;
    public static final String TAG = OverlayService.class.getSimpleName();

    public OverlayService(Context applicationContext) {
        super();
        context = applicationContext;
    }

    public OverlayService() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "[onCreateService]");
        super.onStartCommand(intent, flags, startId);
        KeyguardManager myKM = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
        if(myKM.inKeyguardRestrictedInputMode()) {
            //it is locked
            showOverlayActivity();
        } else {
            //it is not locked
        }
        registerOverlayReceiver();
        context = this;
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterOverlayReceiver();
        Log.i("EXIT", "ondestroy!");
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Intent broadcastIntent = new Intent("ac.in.ActivityRecognition.RestartSensor");
        sendBroadcast(broadcastIntent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void unregisterOverlayReceiver() {
        if (overlayReceiver != null) {
            unregisterReceiver(overlayReceiver);
        }
    }

    private static final String ACTION_DEBUG = "kunal.lockoverlay.action.DEBUG";

    private void registerOverlayReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(ACTION_DEBUG);
        registerReceiver(overlayReceiver, filter);
    }

    private BroadcastReceiver overlayReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "[onReceive]" + action);
            if (action.equals(Intent.ACTION_SCREEN_ON)) {
                // ACTON_SCREEN_ON
                showOverlayActivity();
            } else if (action.equals(ACTION_DEBUG)) {
                showOverlayActivity();
            }
        }
    };

    private void showOverlayActivity() {
        Intent intent = new Intent();
        intent.setClass(this, ActivityClass.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

}

有人可以指出这种行为的一些可能原因,还是可以确定我做错了什么地方?

2 个答案:

答案 0 :(得分:1)

无论您在ClassName.java文件中定义了什么,都将调用所有内容。

您可以重写该方法并在您的类中对其进行定义,以在调用该方法时执行某些操作/功能。

例如,

您可以使用onResume清除数组列表并将更新的元素添加到数组列表(OR),例如,取消应用程序转移到该活动时的所有通知

@Override
public void onResume() {
    super.onResume();
    calendarList.clear();
//dismiss all notifications here
 NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager!=null)
            notificationManager.cancelAll();
}

或例如 onDestroy破坏蓝牙服务,

 @Override
    public void onDestroy() {
        super.onDestroy();
        if (mBluetoothService != null) {
            mBluetoothService.stop();
        }
     }

希望可以澄清。编码愉快!

答案 1 :(得分:0)

关于服务类,您应该调用registerReceiver()或在onCreate中而不是onStartCommand中初始化变量,因为onCreate仅在第一次启动服务时调用一次,而{每次您要通过调用onStartCommand触发对服务的操作时,都会调用{1}}。