Keep activity alive in background until user doesn't kill or close app?

前端 未结 3 699
终归单人心
终归单人心 2021-01-15 10:03

Can I keep Android activity alive in background, until user doesn\'t kill or close the application.

I want to detect user clicks in background some specific hardware

3条回答
  •  时光说笑
    2021-01-15 11:02

    Simply, just create a new service class. Here, using this class you are able to see the Hardware button click event in log. Here i am using volume key as a onKeyEvent.

    public class Xtra extends Service {
    
    
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    
        final BroadcastReceiver vReceiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
                boolean isScreenOn = powerManager.isScreenOn();
    
    
                if (!isScreenOn) {
    
                    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
                    PowerManager.WakeLock mWakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "YourService");
                    mWakeLock.acquire();
                    // The screen has been locked
                    // do stuff...
                    Log.e("Do what you want to do from this service...", "Here screen is off..which do not show or work anything due to screen is off");
    Toast.makeText(getActivity(), "Here screen is off..which do not show or work anything due to screen is off", Toast.LENGTH_SHORT).show();
    
    } else {
                    Log.e("Do what you want to do from this service...", "Here screen is on, works...");
    Toast.makeText(getActivity(), "Here screen is on, works...", Toast.LENGTH_SHORT).show();
    
                }
            }
    
        };
        registerReceiver(vReceiver, new IntentFilter("android.media.VOLUME_CHANGED_ACTION"));
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        return super.onStartCommand(intent, flags, startId);
        //43200000 schedules notification on every 12 hours
    }
    
    
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Log.e("BgService", "::>> Service Stopped...");
        super.onDestroy();
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    }
    

    Make a button click event from your activity to start the service. It will starts your service in background.

    Button btnSave;
    btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
          //From here service starts when you click on a button.
               startService(new Intent(activity, Xtra.class));
            }
        });
    

    Don't forgot to add this service to your manifest.

    
            
                
            
        
    

    When you click on btnSave, it will starts your service. Now just move to home screen and click the volume key, you will surely get the result as it works fine for me.

提交回复
热议问题