android service to start activity not reacting lik

2019-08-21 05:53发布

问题:

I've been working on a program that listens for bluetooth messages to start various apps on my phone. Each of the following work correctly if the main layout is displayed but once one of the commands is called the other does not react properly. Here is the code for the two commands:

if (readMessage.equals("Button"))
{
    Intent i = new Intent(Intent.ACTION_MAIN);
    PackageManager manager = getPackageManager();
    i = manager.getLaunchIntentForPackage("com.google.android.apps.maps");
    Log.d("Main","i is:  "+ i);
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
    Log.d("Main","Map pushed");
}
if (readMessage.equals("Home"))
{
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
    Log.d("Main","Home pushed");
} 

So for example the message home is sent, the app will display the home screen. However if after the home screen is displayed and the message "Button" is sent nothing happens, but I know that the code is called from the Log.

Do I need to put my message handler into it's own service?

Here are the two java files:

Main:

package com.lorenjz.phoneremotefive;

import java.lang.Thread.State;
import java.util.ArrayList;
import java.util.Set;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class MainRemote extends Activity {

    private static TextView btstatus=null;
    private Button closeButt;
    private TextView ringtone=null;
    private TextView checkbox2=null;
    private Boolean serverState = false;

    //declerations:

    private BluetoothAdapter btAdapter;

    public ArrayList myArray;
    public ArrayList devArray;



    private int myContext;
    private BlueStuff mChatService = null;

    // Message types sent from the BluetoothChatService Handler
    public static final int MESSAGE_STATE_CHANGE = 1;
    public static final int MESSAGE_READ = 2;
    public static final int MESSAGE_WRITE = 3;
    public static final int MESSAGE_DEVICE_NAME = 4;
    public static final int MESSAGE_TOAST = 5;

    public static final String DEVICE_NAME = "device_name";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_remote);

        btstatus=(TextView)findViewById(R.id.btstatus);
        closeButt=(Button)findViewById(R.id.close);
        /**checkbox=(TextView)findViewById(R.id.checkbox);
        checkbox2=(TextView)findViewById(R.id.checkbox2);**/

        SharedPreferences prefs=PreferenceManager
                .getDefaultSharedPreferences(this);
        //checkbox.setText(new Boolean(prefs.getBoolean("checkbox", false))
            //    .toString());
        Boolean servState = (new Boolean(prefs.getBoolean("serverstate", false)).booleanValue());
        if (servState = true){
            Log.d("Main","It thinks the server should be on");
            //checkbox2.setText(new Boolean(prefs.getBoolean("checkbox2", false))
                //    .toString());
            mChatService = new BlueStuff(getBaseContext(), mHandler);
            int stateString = mChatService.getState();
            Log.d("Main","chat service:  " + stateString);
            //+ mChatService.getState()
            mChatService.start();
        }
        int stateString = mChatService.getState();
        Log.d("Main","After stuff happens. chat service:  " + stateString);
        btstatus.setText("No Connection yet");
        setupUI();

        closeButt.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {

                mChatService.stop();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

        SharedPreferences prefs=PreferenceManager
                .getDefaultSharedPreferences(this);
        //checkbox.setText(new Boolean(prefs.getBoolean("checkbox", false))
            //    .toString());
        String servState = (new Boolean(prefs.getBoolean("serverstate", false)).toString());
        Log.d("Main","Server State:  " +servState);
        //checkbox2.setText(new Boolean(prefs.getBoolean("checkbox2", false))
            //    .toString());

           // Performing this check in onResume() covers the case in which BT was
        // not enabled during onStart(), so we were paused to enable it...
        // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
        if (mChatService != null) {
            // Only if the state is STATE_NONE, do we know that we haven't started already
            if (mChatService.getState() == BlueStuff.STATE_NONE) {
              // Start the Bluetooth chat services
              mChatService.start();
              startService(new Intent(this, BlueStuff.class));

            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main_remote, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {       
            case R.id.menu_settings:
            startActivity(new Intent(this, PreferencesB.class));        
            return true;      
        }  

        return(super.onOptionsItemSelected(item));
    }


     public void setupUI(){


    }

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_STATE_CHANGE:
                    //if(D) Log.i("Main", "MESSAGE_STATE_CHANGE: " + msg.arg1);
                    switch (msg.arg1) {
                        case BlueStuff.STATE_CONNECTED:
                            Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
                            //setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
                            //mConversationArrayAdapter.clear();
                            break;
                        case BlueStuff.STATE_CONNECTING:
                            Toast.makeText(getApplicationContext(), "Connecting", Toast.LENGTH_SHORT).show();
                            //setStatus(R.string.title_connecting);
                            break;
                        case BlueStuff.STATE_LISTEN:
                            Toast.makeText(getApplicationContext(), "Listening", Toast.LENGTH_SHORT).show();
                            break;
                        case BlueStuff.STATE_NONE:
                            //setStatus(R.string.title_not_connected);
                            break;
                    }
                    break;
                /**case MESSAGE_WRITE:
                    byte[] writeBuf = (byte[]) msg.obj;
                    // construct a string from the buffer
                    String writeMessage = new String(writeBuf);
                    mConversationArrayAdapter.add("Me:  " + writeMessage);
                    break;**/
                case MESSAGE_READ:
                    byte[] readBuf = (byte[]) msg.obj;
                    // construct a string from the valid bytes in the buffer
                    String readMessage = new String(readBuf, 0, msg.arg1);
                    //mConversationArrayAdapter.add(mConnectedDeviceName+":  " + readMessage);
                    //Toast.makeText(getApplicationContext(), "Message:  " + readMessage, Toast.LENGTH_SHORT).show();
                    if (readMessage.equals("Button"))
                    {
                        /**Intent startMain = new Intent(Intent.ACTION_MAIN);
                        startMain.addCategory(Intent.CATEGORY_HOME);
                        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(startMain);**/

                        Intent i = new Intent(Intent.ACTION_MAIN);
                        PackageManager manager = getPackageManager();
                        i = manager.getLaunchIntentForPackage("com.google.android.apps.maps");
                        Log.d("Main","i is:  "+ i);
                        i.addCategory(Intent.CATEGORY_LAUNCHER);
                        startActivity(i);
                        Log.d("Main","Map pushed");

                        /**Intent launch_intent = new Intent("android.intent.action.MAIN");
                        launch_intent.addCategory("android.intent.category.LAUNCHER");
                        launch_intent.setComponent(new ComponentName("com.google.android.maps", "map"));
                        launch_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        startActivity(launch_intent);**/
                    }
                    if (readMessage.equals("Home"))
                    {
                        Intent startMain = new Intent(Intent.ACTION_MAIN);
                        startMain.addCategory(Intent.CATEGORY_HOME);
                        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(startMain);
                        Log.d("Main","Home pushed");
                    }
                    break;
                /**case MESSAGE_DEVICE_NAME:
                    // save the connected device's name
                    mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
                    Toast.makeText(getApplicationContext(), "Connected to "
                                   + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
                    break;
                case MESSAGE_TOAST:
                    Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                                   Toast.LENGTH_SHORT).show();
                    break;**/
            }
        }
    };

    final static void updateBTStatus(int mState){
        Log.d("Main", "update called.  mState is:  " + mState);
        btstatus.setText("prior to switch");
        statusTry();

        switch(mState){
            case 1:
                btstatus.setText("Listening for connection");
                Log.d("Main","Status should be listening for connection");
            case 2:
                btstatus.setText("Connecting");
            case 3:
                btstatus.setText("Connected");
        }

    }

    public static void statusTry(){
        btstatus.setText("post switch try");
    }
}

Blue Stuff:

package com.lorenjz.phoneremotefive;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class BlueStuff extends Service{

    private static final String TAG = "Blue";

    private static final boolean D = true;

    private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private final String NAME = "Zimmer";
    //private BluetoothAdapter mBluetoothAdapter;

    private final BluetoothAdapter mAdapter;
    private final Handler mHandler;
    private int mState;

    public static final int STATE_NONE = 0;       // we're doing nothing
    public static final int STATE_LISTEN = 1;     // now listening for incoming connections
    public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
    public static final int STATE_CONNECTED = 3;  // now connected to a remote device

    private ConnectThread mConnectThread;
    private ConnectedThread mConnectedThread;
    private AcceptThread mSecureAcceptThread;
    private AcceptThread mInsecureAcceptThread;

    /**
     * Constructor. Prepares a new BluetoothChat session.
     * @param context  The UI Activity Context
     * @param handler  A Handler to send messages back to the UI Activity
     */
    public BlueStuff(Context context, Handler handler) {
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mState = STATE_NONE;
        mHandler = handler;
    }

    /**
     * Return the current connection state. */
    public synchronized int getState() {
        return mState;
    }

    /**
     * Start the chat service. Specifically start AcceptThread to begin a
     * session in listening (server) mode. Called by the Activity onResume() */
    public synchronized void start() {
        if (D) Log.d(TAG, "start");

        // Cancel any thread attempting to make a connection
        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

        setState(STATE_LISTEN);


        if (mInsecureAcceptThread == null) {
            mInsecureAcceptThread = new AcceptThread();
            mInsecureAcceptThread.start();
        }
    }

    public void stop(){
        mConnectedThread.cancel();
    }

    /**
     * Set the current state of the chat connection
     * @param state  An integer defining the current connection state
     */
    private synchronized void setState(int state) {
        if (D) Log.d(TAG, "setState() " + mState + " -> " + state);
            mState = state;

        // Give the new state to the Handler so the UI Activity can update
        mHandler.obtainMessage(MainRemote.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
    }

    class AcceptThread extends Thread {
        private final BluetoothServerSocket mmServerSocket;

        public AcceptThread() {

            // Use a temporary object that is later assigned to mmServerSocket,
            // because mmServerSocket is final
            BluetoothServerSocket tmp = null;
            try {
                // MY_UUID is the app's UUID string, also used by the client code

                //tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
            } catch (IOException e) { }
            Log.d("Blue","Listen Failed tmp 1c:  " + tmp);

            mmServerSocket = tmp;
            Log.d("Blue","mmServerSocket 1:  " + mmServerSocket);
            MainRemote.updateBTStatus(mState);
        }

        public void run() {
            Log.d("Blue","Run Called");
            BluetoothSocket socket = null;
            // Keep listening until exception occurs or a socket is returned
            while (true) {
                try {
                    Log.d("Blue","mmServerSocket 2:  " + mmServerSocket);
                    socket = mmServerSocket.accept();
                }
                catch (IOException e) {
                    break;
                }
                // If a connection was accepted
                /** if (socket != null) {
                    // Do work to manage the connection (in a separate thread)
                    //manageConnectedSocket(socket);
                    //mmServerSocket.close();
                    Log.d("Blue","Connection was accepted");
                    MainRemote.updateBTStatus(mState);
                    break;
                }**/
                if (socket != null) {
                    synchronized (BlueStuff.this) {
                        switch (mState) {
                            case STATE_LISTEN:
                            case STATE_CONNECTING:
                                // Situation normal. Start the connected thread.
                                connected(socket, socket.getRemoteDevice());
                                break;
                            case STATE_NONE:
                            case STATE_CONNECTED:
                                // Either not ready or already connected. Terminate new socket.
                                try {
                                    socket.close();
                                } catch (IOException e) {
                                    Log.e(TAG, "Could not close unwanted socket", e);
                                }
                                break;
                        }
                    }
                }
            }
        }

        /** Will cancel the listening socket, and cause the thread to finish */
        public void cancel() {
            try {
                mmServerSocket.close();
            } catch (IOException e) { }
        }
    }

    class ConnectThread extends Thread {
        final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            // Use a temporary object that is later assigned to mmSocket,
            // because mmSocket is final
            BluetoothSocket tmp = null;
            mmDevice = device;

            // Get a BluetoothSocket to connect with the given BluetoothDevice
            try {
                // MY_UUID is the app's UUID string, also used by the server code
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) { }
            mmSocket = tmp;
        }

        public void run() {
            // Cancel discovery because it will slow down the connection
            mAdapter.cancelDiscovery();

            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
            } catch (IOException connectException) {
                // Unable to connect; close the socket and get out
                try {
                    mmSocket.close();
                } catch (IOException closeException) { }
                return;
            }

            // Do work to manage the connection (in a separate thread)
            //manageConnectedSocket(mmSocket);
        }

        /** Will cancel an in-progress connection, and close the socket */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }

    class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) { }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);
                    // Send the obtained bytes to the UI activity
                    mHandler.obtainMessage(MainRemote.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                    Log.d("Blue","message recieved!:  " + bytes);
                } catch (IOException e) {
                    break;
                }
            }
        }

        /* Call this from the main activity to send data to the remote device */
        public void write(byte[] bytes) {
            try {
                mmOutStream.write(bytes);
            } catch (IOException e) { }
        }

        /* Call this from the main activity to shutdown the connection */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }
    /**
     * Start the ConnectedThread to begin managing a Bluetooth connection
     * @param socket  The BluetoothSocket on which the connection was made
     * @param device  The BluetoothDevice that has been connected
     */
    public synchronized void connected(BluetoothSocket socket, BluetoothDevice
            device) {
        //if (D) Log.d(TAG, "connected, Socket Type:" + socketType);

        // Cancel the thread that completed the connection
        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

        // Cancel the accept thread because we only want to connect to one device
        if (mSecureAcceptThread != null) {
            mSecureAcceptThread.cancel();
            mSecureAcceptThread = null;
        }
        if (mInsecureAcceptThread != null) {
            mInsecureAcceptThread.cancel();
            mInsecureAcceptThread = null;
        }

        // Start the thread to manage the connection and perform transmissions
        mConnectedThread = new ConnectedThread(socket);
        mConnectedThread.start();

        // Send the name of the connected device back to the UI Activity
        Message msg = mHandler.obtainMessage(MainRemote.MESSAGE_DEVICE_NAME);
        Bundle bundle = new Bundle();
        bundle.putString(MainRemote.DEVICE_NAME, device.getName());
        msg.setData(bundle);
        mHandler.sendMessage(msg);

        setState(STATE_CONNECTED);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

回答1:

Its not that nothing is happening. Its that the Activity is not recreated. Try overriding onStart() I bet this is getting called instead.