当设备是网络关闭和开启时,如何在应用程序中显示文本消息

时间:2014-09-30 03:51:06

标签: android

public class MainActivity extends ActionBarActivity {
    TextView connectionchecktextbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connectionchecktextbox = (TextView) findViewById(R.id.connectionchecktextbox);

        if (Utils.isNetworkAvailable(MainActivity.this)) {

            connectionchecktextbox.setVisibility(View.GONE);
        }

        else {

            connectionchecktextbox
                    .setText("It Seems Internet Connection if off");
        }

    }

}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.popupdisplay.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/connectionchecktextbox"
        android:layout_width="fill_parent"
        android:layout_height="25dip"
        android:layout_alignParentBottom="true"
        android:background="#F40C0C"
        android:gravity="center"
        android:text="It Seems Internert Connection if off"
        android:textAlignment="gravity"
        android:textColor="#ffffff" />

</RelativeLayout>

这里是Xml

使用此代码,我可以在设备上网和开启时显示短信,但是当我们打开应用程序时显示:表示我们必须始终在创建时调用以显示此项我想要在应用开启时显示自动短信然后短信应该消失,当关闭然后自动显示该短信请告诉我如何应用此

3 个答案:

答案 0 :(得分:1)

公共类NetworkReceiver扩展了BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();

    if (isConnected == true) {

        // initChatHub();
        Toast.makeText(context, "Connected", 1000).show();


    } else {
        Toast.makeText(context, "Disconnected", 1000).show();
    }
}

试试这个简单的代码,如果有任何查询,请尽快通知我。

答案 1 :(得分:0)

我认为你应该使用Toast。

继承人如何使用它。

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

答案 2 :(得分:0)

您可以使用 BroadcastReceiver 执行此操作。广播接收器会通知您任何连接变化:

public class InternetBroadcastReceiver extends BroadcastReceiver 
 public static boolean iAmOnline = false;
{
@Override
public void onReceive(Context context, Intent intent) {

    if (isOnline(context)) 
            {
          iAmOnline = true;
         Toast.makeText(context, "Network Available Do operations",Toast.LENGTH_LONG).show(); 
}else{
        iAmOnline = false;
}


}
public boolean isOnline(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
//should check null because in air plan mode it will be null
        if (netInfo != null && netInfo.isConnected()) {
            return true;
        }
        return false;
    }
}
} 

在清单文件中注册 InternetBroadcastReceiver

<receiver android:name=".InternetBroadcastReceiver" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

您可以在应用中的任何位置使用InternetBroadcastRectiver.iAmOnline检查互联网连接。 :