Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

How to update WiFi state using BroadcastReceiver

This BroadcastReceiver will trigger anytime "Mobile data" disconnects or connects.
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
TextView wifistate = (TextView) findViewById(R.id.infowifi);
this.registerReceiver(mWifiStateChangedReceiver,new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
}


@Override
public void onBackPressed() {
    
    unregisterReceiver(mWifiStateChangedReceiver); // this will unregister Receiver
    this.finish(); // this will finish ThisActivity
    super.onBackPressed();
}
private BroadcastReceiver mWifiStateChangedReceiver 

How to update "Mobile data" state using a BroadcastReceiver

This BroadcastReceiver will trigger anytime "Mobile data" disconnects or connects.
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

this.registerReceiver(mDATAStateChangedReceiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}


@Override
public void onBackPressed() {
    
    unregisterReceiver(mDATAStateChangedReceiver); // this will unregister Receiver
    this.finish(); // this will finish ThisActivity
    super.onBackPressed();
}
private BroadcastReceiver mDATAStateChangedReceiver

How to navigate to another Activity (screen)


In this tutorial you'll learn how to navigate from Activity1 to Activity2 using a button.

1. Create 2 activities. (MainActivity & SecondActivity)
2. In the first Activity layout create a button.
3. In MainActivity.java add below bold codes:

public class MainActivity extends Activity implements OnClickListener {

Button next;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

next = (Button) findViewById(R.id.button1);
next.setOnClickListener(this);

How to Override Menu Button

You can override the default behavior of system key presses by intercepting them in your Activity. This is done by overriding the onKeyDown event, and returning true if you want to prevent the key from being handled by the system.
The code for your case should look something as follows:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ( keyCode == KeyEvent.KEYCODE_MENU ) {

     //Put the code for an action here

     return true;  
  } 

How to write & " ' < > in XML documents - escape characters

Text consists of intermingled character data and markup. In XML documents some characters are used for start-tags, end-tags, entity references, XML declarations and others. This means that if you'll want to use some of those characters in a text writing you'll get errors.

If you want to use these characters in XML documents then you'll need to replace/escape them using numeric character references or using strings.

String replaces:

     "         &quot; 
     '         &apos; 
     <         &lt;