使用BroadcastReceiver实现QQ没网时提示以及点击跳入WIFI设置界面

Java 发表评论

首先看一下xml文件,唯一的:

唯一的activity中代码:
package com.shzz.mail;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseFactory;
import org.apache.http.util.EncodingUtils;

import com.shzz.wms.R;
import com.shzz.wms.action.TGoods.SelTGoodsActivity;
import com.shzz.wms.service.ActivityUtils;
import com.shzz.wms.service.ExitSys;
import com.sun.mail.iap.ResponseInputStream;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.provider.Settings;
import android.text.BoringLayout;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SendMailActivity extends Activity {
private Button send;
private EditText subject;
private EditText body;
private TextView internetInfo;
private brodCastInternet internetStatesFilter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
ActivityUtils.addActivity(this);
setContentView(R.layout.gmail);
send = (Button) findViewById(R.id.btn_send);
subject = (EditText) findViewById(R.id.et_subject);
body = (EditText) findViewById(R.id.et_content);
internetInfo = (TextView) findViewById(R.id.IntentInfo);
//注册网络监听 ,实时监测网络状态
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
internetStatesFilter = new brodCastInternet();
internetInfo.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Settings.ACTION_SETTINGS));
}
});
//注册接收者
registerReceiver(internetStatesFilter, filter);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost(“smtp.163.com”);
mailInfo.setMailServerPort(“25”);
mailInfo.setValidate(true);
mailInfo.setUserName(“wmfffffolution@163.com”); // 你的邮箱地址
mailInfo.setPassword(“123456”);// 您的邮箱密码
mailInfo.setFromAddress(“wmhhhsolution@163.com”); // 发送的邮箱
mailInfo.setToAddress(“zhengchenggong@hhh.cn”); // 发到哪个邮件去
mailInfo.setSubject(“WMF意见反馈”); // 邮件主题
mailInfo.setContent(body.getText().toString()); // 邮件文本
// 这个类主要来发送邮件
SimpleMailSender sms = new SimpleMailSender();
if(!””.equals(body.getText().toString().trim())){
boolean flag = sms.sendTextMail(mailInfo);// 发送文体格式
if(flag == true){
Toast.makeText(SendMailActivity.this, “邮件发送成功”, Toast.LENGTH_SHORT).show();
body.setText(“”);
}else{
Toast.makeText(SendMailActivity.this, “邮件发送失败”, Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(SendMailActivity.this, “请填写反馈内容”, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
}
}
});
//退出
ImageView btnExit = (ImageView) findViewById(R.id.exit);
btnExit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
ExitSys.exitSys(SendMailActivity.this, v);
}
});
}
@Override
protected void onDestroy() {
//取消网络监听
unregisterReceiver(internetStatesFilter);
super.onDestroy();
}
class brodCastInternet extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
boolean success = false;
//获得网络连接服务
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if (State.CONNECTED == state) { // 判断是否正在使用WIFI网络
internetInfo.setVisibility(View.GONE);
send.setEnabled(true);
success = true;
}else{
internetInfo.setVisibility(View.VISIBLE);
send.setEnabled(false);
return;
}
}
}
}

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *