How can close app when WIFI is connected? - android

recently when I click wifi connect button.
showing Connectreceiver.class (activity)
I want when finish wifi connect (=connected state)
finish app (=kill app)
How can I do?
public class Connectingreceiver extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connecting_dialog);
registerReceiver(progressFinish, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
}
private BroadcastReceiver progressFinish = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
if(networkInfo.getState()== NetworkInfo.State.CONNECTED){
moveTaskToBack(true);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
}
};
#Override
protected void onPause(){
super.onPause();
try{
unregisterReceiver(progressFinish);
} catch (IllegalArgumentException e){
e.printStackTrace();
}
}
}

Related

how to broadcast messge when data connection is enable and disable in android

public class MainActivity extends ActionBarActivity {
CheckinternetConnection internet;
TextView textview;
int tempint = 100;
private static final long REPEAT_TIME = 1000 * 5;
private PendingIntent pendingIntent;
Button button1;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.textview);
internet = new CheckinternetConnection();
schedueService();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(internet, filter);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(internet);
}
class CheckinternetConnection extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (Utils.isNetworkAvailable(MainActivity.this)) {
textview.setVisibility(View.GONE);
startService(new Intent(getBaseContext(), myserveclass.class));
schedueService();
//setMobileDataEnabled(getApplicationContext(), true);
} else {
textview.setVisibility(View.VISIBLE);
textview.setText("It Seems Internet Connection is off");
stopService(new Intent(getBaseContext(), myserveclass.class));
CancelAlarm();
}
}
}
this is my code using this code i am able to display Connected and disconnected when application Launch i want as i Enable data connection from Setting or Top of device then there should show Data is connected and as i will Off data connection then it should display data is not connected actully i want start service when My application has network connection and stop service when notwork is not connected suggest me how to implement this.
Try with this receiver
Also make sure you have the permissions in the Manifest
class CheckinternetConnection extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conn = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = conn.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected==true)
textview.setVisibility(View.GONE);
startService(new Intent(getBaseContext(), myserveclass.class));
schedueService();
//setMobileDataEnabled(getApplicationContext(), true);
} else {
textview.setVisibility(View.VISIBLE);
textview.setText("It Seems Internet Connection is off");
stopService(new Intent(getBaseContext(), myserveclass.class));
CancelAlarm();
}
}
}

Notify to the active activity from BroadcastReceiver

I want to show a TextView to current active activity if internet is not availabe.
I am getting internet state but how to notify it to current active activity (In which activity my concentration is).
public class NetworkChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent)
{
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable() || mobile.isAvailable()) {
Log.d("TAG", "Netowk Available");
} else {
//notify it to current active activity and show a textview as
//"No internet connection"
}
}
}
In your activity:
public class YourActivity extends Activity {
private TextView mTextView;
public static final String NETWORK_DISABLE_ACTION = "yourpackagename.action.network_disbale";
private LocalBroadcastManager mLocalBroadcastManager;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(NETWORK_DISABLE_ACTION)) {
mTextView.setText("No internet connection");
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_xml);
mTextView = (TextView) findViewById(R.id.your_textView_id);
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(NETWORK_DISABLE_ACTION);
mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, intentFilter);
}
#Override
protected void onDestroy() {
super.onDestroy();
mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);
}
}
and in your NetworkChangeReceiver:
if (wifi.isAvailable() || mobile.isAvailable()) {
Log.d("TAG", "Netowk Available");
} else {
//notify it to current active activity and show a textview as
//"No internet connection"
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(
YourActivity.NETWORK_DISABLE_ACTION ));
}

Creating my own action for BroadcastReceiver

I have an method like this:
public static boolean checkConnection(){
if(getConnection()!=null){
return true;
}else
return false;
}
I trying to listen to the result of this method throughout my application. Till my application is alive. Only inside my application
How should I create my own action so that in BroadcastReceiver I can listen to this method and show an dialog when it returns false and hide the dialog automatically when it start returning true?
How and what will be the best approach to do this?
public class BroadCastActivity extends Activity implements OnClickListener{
ConnectionReceverLocal mReceverLocal;
Button mSwithcOn,mSwitchOff;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broad_cast);
/*Intent intent = new Intent();
intent.setAction("com.broadcast.myconnectionbroadcast");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);*/
mReceverLocal = new ConnectionReceverLocal();
mSwitchOff = (Button)findViewById(R.id.switchoff);
mSwithcOn = (Button)findViewById(R.id.switchon);
mSwitchOff.setOnClickListener(this);
mSwithcOn.setOnClickListener(this);
}
private void sendMessage() {
new AsyncTask<Void, Void, Boolean>() {
#Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
Log.d("Connection result Checking Asynctasks", ""+ConnectionProvider.checkConnection());
return ConnectionProvider.checkConnection();
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Intent intent = new Intent("com.broadcast.myconnectionbroadcast");
// Add data
intent.putExtra("message", result);
LocalBroadcastManager.getInstance(BroadCastActivity.this).sendBroadcast(intent);
}
}.execute();
}
#Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceverLocal);
}
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mReceverLocal,
new IntentFilter("com.alignminds.broadcast.myconnectionbroadcast"));
sendMessage();
}
public class ConnectionReceverLocal extends BroadcastReceiver {
public ConnectionReceverLocal() {
}
#Override
public void onReceive(Context context, Intent intent) {
AlertDialog.Builder mDBuilder = new AlertDialog.Builder(BroadCastActivity.this);
Boolean intentAction = intent.getBooleanExtra("message", false);
if(intentAction==false)
{
Log.d("Connection is false in broadcast recever", "Connection is false");
if(mDBuilder!=null)
{
mDBuilder.setMessage("No Network");
mDBuilder.create().show();
}
}else {
Log.d("Connection is true in broadcast recever", "Connection is true");
if(mDBuilder!=null)
{
mDBuilder.setMessage("Network Ok");
mDBuilder.create().show();
}
}
}
}
#Override
public void onClick(View v) {
if(v==mSwithcOn)
{
WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
}
if(v==mSwitchOff)
{
WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
}
}
}
Send a broadcast with whatever action you want like this
Intent intent = new Intent();
intent.setAction("my_fancy_action");
intent.putExtra(EVENT_MESSAGE, message);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
Subclass BroadcastReceiver class to listen for broadcasts
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
// Do something
}
Register your receiver to listen to the broadcast
IntentFilter filter = new IntentFilter();
filter.addAction("my_fancy_action");
LocalBroadcastManager.getInstance(context).registerReceiver(myReceiver, filter);

ANDROID - Crash when I call sendBroadcast to check the network

I have a broadcast to check the network, when the network changes state, I notify my activity with onNetworkActivated() or onNetworkInactivated():
public class NetworkBroadcastReceiver extends BroadcastReceiver
{
private OnNetworkListener currentActivity = null;
public NetworkBroadcastReceiver(FragmentActivity activity)
{
try
{
currentActivity = (OnNetworkListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + " doit implémenter OnNetworkListener");
}
}
#Override
public void onReceive(Context context, Intent intent)
{
ConnectivityManager connectivity = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null)
{
currentActivity.onNetworkInactivated();
return;
}
else
{
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.getState() == NetworkInfo.State.CONNECTED)
{
currentActivity.onNetworkActivated();
return;
}
}
currentActivity.onNetworkInactivated();
}
public interface OnNetworkListener
{
public void onNetworkActivated();
public void onNetworkInactivated();
}
}
In my Activity, when I I need to call for various reasons sendBroadcast the first time, but Android crash :s
public class Earthquake extends FragmentActivity implements OnNetworkListener
{
private FragmentManager fragmentManager;
private NetworkBroadcastReceiver networkbroadcastreceiver;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fragmentManager = getSupportFragmentManager();
networkbroadcastreceiver = new NetworkBroadcastReceiver(Earthquake.this);
this.registerReceiver(networkbroadcastreceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
this.sendBroadcast(new Intent(ConnectivityManager.CONNECTIVITY_ACTION));
}
#Override
protected void onDestroy()
{
this.unregisterReceiver(networkbroadcastreceiver);
super.onDestroy();
}
public void onNetworkActivated()
{
Log.e("onNetworkActivated", "onNetworkActivated");
}
public void onNetworkInactivated()
{
Log.e("onNetworkInactivated", "onNetworkInactivated");
}
}
What is the problem ?
Thank you in advance
https://stackoverflow.com/a/17906435/2201919
By #JoxTraex
You are not allowed to send this broadcast. If applications could send this broadcast then this could cause problems on the device. This is a protected system broadcast.
Please approach your problem in a different way.
Refer: http://developer.android.com/reference/android/net/ConnectivityManager.html
Notice how there are only a few methods that use that action. This implies you cannot send this broadcast.

Notify network status in same activity using Receivers

I am working with google chat application
When chatting if network fails, I have to notify user that network disconnected and if network connected again i have to notify network connected in the same activity.But not worked out
Following is the code which I written:
public class GTChat extends ListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
NetworkInfo info = (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (info.getState().equals(NetworkInfo.State.CONNECTED))
{
Toast.makeText(getApplicationContext(), "Network connected",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Network not connected",Toast.LENGTH_LONG).show();
}
}
};
protected void onPause()
{
unregisterReceiver(mConnReceiver);
super.onPause();
}
protected void onResume()
{
registerReceiver(mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
super.onResume();
}
call the super.onResume()/super.onPause() before to register/unregistering receiver.
protected void onPause()
{
super.onPause();
unregisterReceiver(mConnReceiver);
}
protected void onResume()
{
super.onResume();
registerReceiver(mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}

Categories

Resources