Click here to Skip to main content
15,879,184 members
Home / Discussions / Android
   

Android

 
QuestionHow to broadcast audio 3gp format over wifi converting audio file in udp packets? Pin
Member 1114560021-Jun-18 18:36
Member 1114560021-Jun-18 18:36 
SuggestionRe: How to broadcast audio 3gp format over wifi converting audio file in udp packets? Pin
Richard MacCutchan21-Jun-18 21:03
mveRichard MacCutchan21-Jun-18 21:03 
QuestionFetching JSON data in recyclerview using retrofit and implement scroll to load or infinitescroll Pin
Arindam Mukherjee20-Jun-18 19:29
Arindam Mukherjee20-Jun-18 19:29 
QuestionCurrent Android Studio // Is it 2017 ? Pin
C-P-User-313-Jun-18 22:41
C-P-User-313-Jun-18 22:41 
AnswerRe: Current Android Studio // Is it 2017 ? Pin
Richard Deeming14-Jun-18 2:36
mveRichard Deeming14-Jun-18 2:36 
AnswerRe: Current Android Studio // Is it 2017 ? Pin
Ganza Charles20-Jun-18 22:42
Ganza Charles20-Jun-18 22:42 
GeneralRe: Current Android Studio // Is it 2017 ? Pin
C-P-User-311-Nov-18 20:46
C-P-User-311-Nov-18 20:46 
Question(ANDROID) -->How can data be send from the run method of a thread in a service to a client using Handler and Messeger? Pin
Member 119110659-Jun-18 22:36
Member 119110659-Jun-18 22:36 
Quote:
I'm writing a program using bound service with Inter Process Communication(IPC). The goal is to send the generated data in a thread (DataExchange) from the service to the mainActivity, using Messenger and Hanler. When the data is placed in the message queue from run method, I'm unable to receive it in the activityHandler (mainActivity). fortunatly I can receive the data in the serviceHandler (Local communication), however when I try to send the same message from the serviceHandler (service) to activityHandler (mainActivity), I'm getting a FATAL EXCETION
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Messenger.send(android.os.Message)' on a null object reference
Someone please help



MainActivity

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private static final int UPDATE = 2;

    TextView textView;
    Messenger activityMessenger;
    boolean bound = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R. id. textView);

        Intent bindBtService = new Intent(this, BluetoothService.class);
        bindService(bindBtService, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            activityMessenger = new Messenger(service);
            bound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            unbindService(serviceConnection);
            activityMessenger = null;
            bound = false;

        }
    };

    /*To process Incoming massages from the service*/
    class activityHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            String received;
            switch (msg.what){
                case UPDATE:
                    received = msg.getData().getString("update");
                    //for debugging
                    Log.i(TAG, "From Run Method " + received);
                    textView.setText(received);
                    break;

                default:
                    super.handleMessage(msg);
            }
        }
    }

    @Override
    protected void onDestroy() {
        unbindService(serviceConnection);
        activityMessenger = null;
        bound = false;
        super.onDestroy();
    }
}


Service

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;

import java.util.Random;

public class BluetoothService extends Service {
    private  static final String TAG = BluetoothService.class.getSimpleName();
    private static final int SEND = 1;
    private static final int UPDATE = 2;

    private DataExchange dataExchange;

    //Messenger serviceMessenger;
    Messenger serviceMessenger = new Messenger(new ServiceHandler());
    public BluetoothService() {
        //Log.i(TAG, "Inside CONSTRUCTOR");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //Log.i(TAG, "Inside ONCREATE");
        //serviceMessenger = new Messenger(new ServiceHandler());

        //create a new thread with a serviceHandler object
        dataExchange = new DataExchange(new ServiceHandler());
        dataExchange.start();
    }

    /*Pass the binding object Messenger with a reference of the serviceHandler
     *to perform IPC communication
    */
    @Override
    public IBinder onBind(Intent intent) {
        return serviceMessenger.getBinder();
    }

    class ServiceHandler extends Handler{
        Messenger mess;
        Message message;
        String response = " ";
        Bundle bundle = new Bundle();

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){

                /*GET THE DATA FROM RUN METHOD AND SEND IT TO  mainActivity*/
                case SEND:
                    response = msg.getData().getString("message");
                    //Log for debug proposes
                    Log.i(TAG, "Random Number: " + response );
                    /*Initiate message object with a identifier*/
                    message = Message.obtain(null, UPDATE);
                    //Put the  and a keyword and the string message into a bundle
                    bundle.putString("update", response);
                    //Pass the bundle to the message object
                    message.setData(bundle);
                        try {
                            /* place the message into the message queue
                             * replyTo returns a reference from the receive Handler
                             * in this case the receive handler is the activityHandle
                            */
                           msg.replyTo.send(message);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                        break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    private class DataExchange extends Thread implements Runnable{
        Handler serviceHandler;
        public DataExchange(ServiceHandler serviceHandler) {
            super();
            //new object of the handler class to process communication
            this.serviceHandler = serviceHandler;
        }

        @Override
        public void run() {

            for (int i = 0; i < 100; i++){
                String val = String.valueOf(i);

                try {
                    Thread.sleep(1000);
                    Message msg = prepareMessage(val);
                    //place message on the messageQueue
                    //serviceHandler.sendMessage(msg);

                    try {
                        serviceMessenger.send(msg);

                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        /*prepare the message with a identifier, a key word and a string message
        * id : UPDATE and key word :update defines a message to be sent
        * to the mainActivity (Inter Process Communication "IPC")
        * id : SEND and key word :message defines a message to be sent
        * to the serviceHandler (Local communication)
        * */
        private Message prepareMessage(String string){

            Bundle dataBundle = new Bundle();

            /*For IPC communication*/
            //Message result = serviceHandler.obtainMessage(UPDATE);
            //dataBundle.putString("update", string);

            /*For LOCAL communication*/
            Message result = serviceHandler.obtainMessage(SEND);
            dataBundle.putString("message", string);
            //result.replyTo = new Messenger(new ServiceHandler());
            result.setData(dataBundle);

            return result;
        }
    }
}


Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.seber.lisah">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".BluetoothService"
            android:enabled="true"
            android:exported="true"></service>
    </application>

</manifest>

AnswerRe: (ANDROID) -->How can data be send from the run method of a thread in a service to a client using Handler and Messeger? Pin
Richard MacCutchan10-Jun-18 4:18
mveRichard MacCutchan10-Jun-18 4:18 
GeneralRe: (ANDROID) -->How can data be send from the run method of a thread in a service to a client using Handler and Messeger? Pin
Member 1191106510-Jun-18 9:24
Member 1191106510-Jun-18 9:24 
GeneralRe: (ANDROID) -->How can data be send from the run method of a thread in a service to a client using Handler and Messeger? Pin
Richard MacCutchan10-Jun-18 22:06
mveRichard MacCutchan10-Jun-18 22:06 
SuggestionRe: (ANDROID) -->How can data be send from the run method of a thread in a service to a client using Handler and Messeger? Pin
David Crow11-Jun-18 2:16
David Crow11-Jun-18 2:16 
QuestionAddition,Sub,Mul & divide issue Pin
Member 1126111131-May-18 2:22
Member 1126111131-May-18 2:22 
AnswerRe: Addition,Sub,Mul & divide issue Pin
Richard MacCutchan2-Jun-18 2:44
mveRichard MacCutchan2-Jun-18 2:44 
QuestionXamarin WebView please help. Pin
Member 1335044424-May-18 6:03
Member 1335044424-May-18 6:03 
AnswerRe: Xamarin WebView please help. Pin
David Crow24-May-18 9:34
David Crow24-May-18 9:34 
GeneralRe: Xamarin WebView please help. Pin
Member 1335044425-May-18 11:07
Member 1335044425-May-18 11:07 
QuestionRe: Xamarin WebView please help. Pin
David Crow28-May-18 15:59
David Crow28-May-18 15:59 
QuestionAndroid code obfuscating Pin
AndroidVH21-May-18 23:14
AndroidVH21-May-18 23:14 
SuggestionRe: Android code obfuscating Pin
Jochen Arndt21-May-18 23:54
professionalJochen Arndt21-May-18 23:54 
GeneralRe: Android code obfuscating Pin
AndroidVH22-May-18 2:23
AndroidVH22-May-18 2:23 
GeneralRe: Android code obfuscating Pin
Jochen Arndt22-May-18 2:53
professionalJochen Arndt22-May-18 2:53 
GeneralRe: Android code obfuscating Pin
AndroidVH22-May-18 18:18
AndroidVH22-May-18 18:18 
AnswerRe: Android code obfuscating Pin
David Crow22-May-18 2:28
David Crow22-May-18 2:28 
Questionhow to append new elements in to an array list in android? Pin
rathilesh c20-May-18 0:10
rathilesh c20-May-18 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.