Boot/ScreenOn Broadcast Receiver not working

后端 未结 2 827
我在风中等你
我在风中等你 2021-01-06 00:14

I have a blank HelloWorld Application:

package tutorials.TestReceivers;

import android.app.Activity;
import android.os.Bundle;

public class TestReceiversAc         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-06 00:31

    After a long time of frustration, I solved the problem above.

    The right way to register Boot Broadcast Receiver (and open activity according to it), is:

    Blank HelloWorld Application (TestReceiversActivity.java):

    package tutorials.TestReceivers;
    
    import android.app.Activity;
    public class TestReceiversActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }
    

    Another Boot Receiver Class (BootReceiver.java)

    package tutorials.TestReceivers;
    
    import android.content.BroadcastReceiver;
    public class BootReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
             Log.d("DAVID", "Hi, Boot reciver was catch!");
             Intent i = new Intent(context, TestReceiversActivity.class);
             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             context.startActivity(i);
           }
    }
    

    Note: You must set the flag to make it work!

    Set the manifest to:

    
    
        
    
        
        
                
                
                    
                
            
            
                    
                        
                        
                    
            
        
     
    

    Enjoy!

提交回复
热议问题