0

In my android project there is one activity and on service. in the main layout i have three control : 2 buttons (start and stop button) and a TextView (for the result).when i click start button the service will start and sends a text message via localbroadcastmanager to the main activity and activity must show the message in the TextView but this is not happening. i also should add that i have added Android Support Library v4 to my project and i receive no errors and crashes when i run the application. i have tested these code on a real device and genymotion but i didn't get the desired result. i would if you tell what is wrong with my codes: Here is my Activity code :

package ir.sanatnegar.lbcm;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class LBCMActivity extends Activity {

    Button   btnStart;
    Button   btnStop;
    TextView tvMessage;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("SAED"));
        btnStart = (Button) findViewById(R.id.btnStart);
        btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                startService(new Intent(LBCMActivity.this, MyService.class));

            }
        });
        btnStop = (Button) findViewById(R.id.btnStop);
        btnStop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                stopService(new Intent(LBCMActivity.this, MyService.class));

            }
        });

    }


    @Override
    protected void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
        super.onPause();
    }


    @Override
    protected void onResume() {
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("SAED"));

        super.onResume();
    }

    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {

                                                   @Override
                                                   public void onReceive(Context context, Intent intent) {
                                                       Log.i("LOG", "Message Received ... ");
                                                       String message = intent.getStringExtra("message");
                                                       tvMessage.setText(message);

                                                   }
                                               };
}

// The Service Code :

package ir.sanatnegar.lbcm;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;


public class MyService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LOG", "onStartCommand occured");
        sendMessage();
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        Log.i("LOG", "onDestroty occured");
        super.onDestroy();
    }


    private void sendMessage()
    {
        Log.i("LOG", "Broadcasting Message ...");
        Intent intent = new Intent("SAED");
        intent.putExtra("message", "This is a message from MyService!!!");
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

    }

}

2 Answers2

0

I do not see anything wrong in your code, except the missing assignment of tvMessage.

tvMessage=(TextView)findViewById(R.id.msg);

but it should give you a runtime exception at this line --

tvMessage.setText(message);

I tried the code and works perfect on my phone, and textbox displays "This is a message from MyService" on start button click.

Nicks
  • 16,030
  • 8
  • 58
  • 65
  • you were right my friend i forgot to declare tvMessage . i added the declaration but i am not receiving the message yet. :-( do you have any solution ? – Sanatnegar Aug 29 '15 at 15:02
  • I copied the complete code, and it is working fine. It should work now. show your logcat and your layout file. Can you see the textbox well placed. – Nicks Aug 29 '15 at 15:11
0

Here is my manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ir.sanatnegar.lbcm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".LBCMActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService" android:process=":my_service" />
    </application>

</manifest>
  • I got the solution ... the service definition in manifest was wrong ... i remove : android:process=":my_service" stament from service declaration. anyway thanks for you attention – Sanatnegar Aug 29 '15 at 15:10
  • because of android:process tag, the service was launched in a separate process of its own and so that process was not updating your main UI thread. Glad, if I was of some help. – Nicks Aug 29 '15 at 15:22