Added exit button, reformatted strings.xml.

This commit is contained in:
Felix Ableitner 2014-10-17 01:48:49 +03:00
parent 199f2e703d
commit 7ca15109c0
5 changed files with 38 additions and 8 deletions

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/exit"
android:title="Exit" />
</menu>

View file

@ -5,10 +5,19 @@
<string name="app_name">EnsiChat</string>
<!-- ContactsFragment -->
<!-- MainActivity -->
<string name="no_contacts_found">No contacts found :(</string>
<!-- Toast shown if user denies request to enable bluetooth -->
<string name="bluetooth_required">Bluetooth is required for this app.</string>
<!-- Menu item to close app and stop service -->
<string name="exit">Exit</string>
<!-- ContactsFragment -->
<!-- Empty text for contacts list -->
<string name="no_contacts_found">No contacts found :(</string>
</resources>

View file

@ -4,8 +4,10 @@ import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content._
import android.os.Bundle
import android.view.{MenuItem, Menu}
import android.widget.Toast
import com.nutomic.ensichat.R
import com.nutomic.ensichat.bluetooth.ChatService
class MainActivity extends Activity {
@ -14,10 +16,16 @@ class MainActivity extends Activity {
override def onCreate(savedInstanceState: Bundle): Unit = {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
startService(new Intent(this, classOf[ChatService]))
val intent: Intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(intent, REQUEST_ENABLE_BLUETOOTH)
}
override def onCreateOptionsMenu(menu: Menu): Boolean = {
getMenuInflater().inflate(R.menu.main, menu)
return true
}
override def onActivityResult(requestCode: Int, resultCode: Int, data: Intent): Unit = {
requestCode match {
case REQUEST_ENABLE_BLUETOOTH =>
@ -28,4 +36,15 @@ class MainActivity extends Activity {
}
}
override def onOptionsItemSelected(item: MenuItem): Boolean = {
item.getItemId match {
case R.id.exit =>
stopService(new Intent(this, classOf[ChatService]))
finish()
return true
case _ =>
return false
}
}
}

View file

@ -39,11 +39,6 @@ class ChatService extends Service {
return mBinder
}
override def onDestroy(): Unit = {
super.onDestroy()
unregisterReceiver(mReceiver)
}
def doDiscovery() {
// If we're already discovering, stop it.
if (mBluetoothAdapter.isDiscovering()) {

View file

@ -44,7 +44,6 @@ class ContactsFragment extends Fragment with DeviceListener {
override def onCreate(savedInstanceState: Bundle): Unit = {
super.onCreate(savedInstanceState)
getActivity.startService(new Intent(getActivity, classOf[ChatService]))
getActivity.bindService(new Intent(getActivity, classOf[ChatService]),
mChatServiceConnection, Context.BIND_AUTO_CREATE)
}