Creating a Broadcast Receiver in android

hey guys ,

In this tutorial  i am going to show how to create a simple broadcast receiver ,which makes your phone vibrate when the charger is connected



1 step:


create an android project as you do always then create a class wich extends broadcast receiver class


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;

public  class Vibrated extends BroadcastReceiver{

 @Override
 public void onReceive(Context context, Intent arg1) {
    Toast.makeText(context, "Don't panik but your time is up!!!!.",
           Toast.LENGTH_LONG).show();
       // Vibrate the mobile phone
       Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 
vibrator.vibrate(4000);
       Toast.makeText(context, "vibrated!!!!.",
            Toast.LENGTH_LONG).show();
 
  
 }

}



2 step:


in your androidmanifest.xml file add the vibrator permission and add register your receiver



    
 
    
    
        
            
                

                
            
        
     
         
            
                
            
        
    




thats pretty much it when the charger is connected it invokes the onReceive method on your receiver

Comments