1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
| package com.bjtcrj.gms9x;
import android.annotation.TargetApi; import android.app.AppOpsManager; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.content.pm.ApplicationInfo; import android.net.Uri; import android.os.Build; import android.os.IBinder; import android.provider.Settings; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView;
import com.yxc.websocketclientdemo.adapter.Adapter_ChatMessage; import com.yxc.websocketclientdemo.im.JWebSocketClient; import com.yxc.websocketclientdemo.im.JWebSocketClientService; import com.yxc.websocketclientdemo.modle.ChatMessage; import com.yxc.websocketclientdemo.util.Util;
import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Context mContext; private JWebSocketClient client; private JWebSocketClientService.JWebSocketClientBinder binder; private JWebSocketClientService jWebSClientService; private EditText et_content; private ListView listView; private Button btn_send; private List<ChatMessage> chatMessageList = new ArrayList<>(); private Adapter_ChatMessage adapter_chatMessage; private ChatMessageReceiver chatMessageReceiver;
private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.e("MainActivity", "服务与活动成功绑定"); binder = (JWebSocketClientService.JWebSocketClientBinder) iBinder; jWebSClientService = binder.getService(); client = jWebSClientService.client; }
@Override public void onServiceDisconnected(ComponentName componentName) { Log.e("MainActivity", "服务与活动成功断开"); } };
private class ChatMessageReceiver extends BroadcastReceiver{
@Override public void onReceive(Context context, Intent intent) { String message=intent.getStringExtra("message"); ChatMessage chatMessage=new ChatMessage(); chatMessage.setContent(message); chatMessage.setIsMeSend(0); chatMessage.setIsRead(1); chatMessage.setTime(System.currentTimeMillis()+""); chatMessageList.add(chatMessage); initChatMsgListView(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); setContentView(R.layout.activity_main); mContext=MainActivity.this; startJWebSClientService(); bindService(); doRegisterReceiver(); checkNotification(mContext); findViewById(); initView(); }
private void bindService() { Intent bindIntent = new Intent(mContext, JWebSocketClientService.class); bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE); }
private void startJWebSClientService() { Intent intent = new Intent(mContext, JWebSocketClientService.class); startService(intent); }
private void doRegisterReceiver() { chatMessageReceiver = new ChatMessageReceiver(); IntentFilter filter = new IntentFilter("com.bjtcrj.servicecallback.content"); registerReceiver(chatMessageReceiver, filter); }
private void findViewById() { listView = findViewById(R.id.chatmsg_listView); btn_send = findViewById(R.id.btn_send); et_content = findViewById(R.id.et_content); btn_send.setOnClickListener(this); } private void initView() { et_content.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if (et_content.getText().toString().length() > 0) { btn_send.setVisibility(View.VISIBLE); } else { btn_send.setVisibility(View.GONE); } }
@Override public void afterTextChanged(Editable editable) {
} }); }
@Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_send: String content = et_content.getText().toString(); if (content.length() <= 0) { Util.showToast(mContext, "消息不能为空哟"); return; }
if (client != null && client.isOpen()) { jWebSClientService.sendMsg(content);
ChatMessage chatMessage=new ChatMessage(); chatMessage.setContent(content); chatMessage.setIsMeSend(1); chatMessage.setIsRead(1); chatMessage.setTime(System.currentTimeMillis()+""); chatMessageList.add(chatMessage); initChatMsgListView(); et_content.setText(""); } else { Util.showToast(mContext, "连接已断开,请稍等或重启App哟"); } break; default: break; } }
private void initChatMsgListView(){ adapter_chatMessage = new Adapter_ChatMessage(mContext, chatMessageList); listView.setAdapter(adapter_chatMessage); listView.setSelection(chatMessageList.size()); }
private void checkNotification(final Context context) { if (!isNotificationEnabled(context)) { new AlertDialog.Builder(context).setTitle("温馨提示") .setMessage("你还未开启系统通知,将影响消息的接收,要去开启吗?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { setNotification(context); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {
} }).show(); } }
private void setNotification(Context context) { Intent localIntent = new Intent(); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS"); localIntent.putExtra("app_package", context.getPackageName()); localIntent.putExtra("app_uid", context.getApplicationInfo().uid); } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); localIntent.addCategory(Intent.CATEGORY_DEFAULT); localIntent.setData(Uri.parse("package:" + context.getPackageName())); } else { localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT >= 9) { localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); localIntent.setData(Uri.fromParts("package", context.getPackageName(), null)); } else if (Build.VERSION.SDK_INT <= 8) { localIntent.setAction(Intent.ACTION_VIEW); localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails"); localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName()); } } context.startActivity(localIntent); }
@TargetApi(Build.VERSION_CODES.KITKAT) private boolean isNotificationEnabled(Context context) {
String CHECK_OP_NO_THROW = "checkOpNoThrow"; String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); ApplicationInfo appInfo = context.getApplicationInfo(); String pkg = context.getApplicationContext().getPackageName(); int uid = appInfo.uid;
Class appOpsClass = null; try { appOpsClass = Class.forName(AppOpsManager.class.getName()); Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class); Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (Integer) opPostNotificationValue.get(Integer.class); return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
} catch (Exception e) { e.printStackTrace(); } return false; }
}
|