@Override public int onStartCommand(Intent intent, int flags, int startId) { instance = this; Log.i("Jane", "Received start id " + startId + ": " + intent); Log.d("Jane", "Service received start command."); tts = new TextToSpeech( this, new TextToSpeech.OnInitListener() { @Override public void onInit(int status) {} }); tts.setOnUtteranceCompletedListener(this); // set up Bluetooth here if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { bt = new Gingerbread(); } else { bt = new Honeycomb(); } bt.setContext(getApplicationContext()); try { bt.getProxy(); } catch (Exception e) { e.printStackTrace(); } AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ComponentName mediaButtonResponder = new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()); am.registerMediaButtonEventReceiver(mediaButtonResponder); PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); WakeLock lock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "JaneLock"); lock.acquire(10 * 60 * 1000); localBroadcastManager = LocalBroadcastManager.getInstance(this); localChatReceiver = new ChatReceiver(this); String username = intent.getExtras().getString("username"); String password = intent.getExtras().getString("password"); uiMessenger = intent.getExtras().getParcelable("messenger"); nameCache = new HashMap<String, String>(); chatCache = new HashMap<String, Chat>(); smack = org.jivesoftware.smack.SmackAndroid.init(this); this.registerReceiver(stateIntents, new IntentFilter(JaneIntent)); this.registerReceiver(stateIntents, new IntentFilter(Bluetooth.BLUETOOTH_STATE)); LoginThread login = new LoginThread(username, password, this); login.start(); return START_STICKY; }
/** 提交账号密码信息到服务器 */ private void submit() { String accounts = mAccounts.getText().toString(); String password = mPassword.getText().toString(); // accounts = "test"+accounts+"@"+FriendListActivity.SERVICE_NAME ; // password = "******"; if (accounts.length() == 0 || password.length() == 0) { DialogFactory.ToastDialog(this, "notice", "password cann not be empty"); } else { try { SmackAndroid.init(LoginActivity.this); Log.i("tong test", "accounts : " + accounts + " password:"******"notice", "login success"); // 跳转到好友列表 Intent intent = new Intent(); intent.putExtra("USERID", accounts); intent.setClass(LoginActivity.this, FriendListActivity.class); startActivity(intent); } catch (XMPPException e) { XmppConnection.closeConnection(); handler.sendEmptyMessage(2); e.printStackTrace(); } } }
private SmackHelper(Context context) { this.context = context; smackAndroid = SmackAndroid.init(context); messagePacketListener = new MessagePacketListener(context); presencePacketListener = new PresencePacketListener(context); SmackConfiguration.setDefaultPacketReplyTimeout(20 * 1000); Roster.setDefaultSubscriptionMode(SubscriptionMode.manual); }
public void connect() { // Create a connection SmackAndroid.init(mParent); int DNSSRV_TIMEOUT = 1000 * 30; // 30s try { SASLAuthentication.supportSASLMechanism("PLAIN", 0); AndroidConnectionConfiguration conf = new AndroidConnectionConfiguration(SERVICE, DNSSRV_TIMEOUT); conf.setTruststoreType("AndroidCAStore"); conf.setTruststorePassword(null); conf.setTruststorePath(null); mConnection = new XMPPConnection(conf); new Thread( new Runnable() { @Override public void run() { try { mConnection.connect(); Log.i(TAG, "Connected to " + mConnection.getHost()); mConnection.login(mUsername, mPassword); Log.i(TAG, "Logged in as " + mConnection.getUser()); // Set the status to available Presence presence = new Presence(Presence.Type.available); mConnection.sendPacket(presence); mConnection.addPacketListener( new PacketListener() { @Override public void processPacket(Packet packet) { Message message = (Message) packet; if (message.getBody() != null) { mMessageListener.onMessageReceived( message.getFrom(), message.getBody()); } } }, new MessageTypeFilter(Message.Type.chat)); } catch (XMPPException ex) { Log.e(TAG, "Failed to connect/login as " + mUsername); Log.e(TAG, ex.toString()); mConnection = null; } } }) .start(); } catch (Exception e) { // TODO(clchen): Auto-generated catch block e.printStackTrace(); } }
private void disconnect() { // All the null checks are necessary because this method is run when an account is added // from out of the app as well try { if (mLoginTask != null) mLoginTask.get(); // Disconnecting in the middle of a login may be troublesome } catch (InterruptedException | ExecutionException e) { Crashlytics.logException(e); } try { if (api != null) { api.disconnect(); api = null; } } catch (SmackException.NotConnectedException e) { Crashlytics.logException(e); } if (mSmackAndroid != null) mSmackAndroid.onDestroy(); isConnected = Boolean.FALSE; }
@Override public void onDestroy() { // Cancel the persistent notification. notificationManager.cancel(JANE_NOTIFICATION_CODE); // disconnect bluetooth proxy try { bt.releaseProxy(); } catch (Exception e) { e.printStackTrace(); } // close smack AsyncTask<Void, Void, Integer> smackShutdown = new AsyncTask<Void, Void, Integer>() { @Override protected Integer doInBackground(Void... params) { connection.disconnect(); return 1; } }; smackShutdown.execute(); smack.onDestroy(); // unregister JaneIntent receiver this.unregisterReceiver(stateIntents); // unregister tts tts.shutdown(); // Tell the user we stopped. Toast.makeText(this, "Jane service stopped.", Toast.LENGTH_SHORT).show(); super.onDestroy(); }
public void onDestroy() { cleanupConnection(); smackAndroid.onDestroy(); }