Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:44:09+00:00 2026-06-15T11:44:09+00:00

I want to run multiple activities in BroadcastReacevier. But its not working, Only two

  • 0

I want to run multiple activities in BroadcastReacevier. But its not working,
Only two activities are getting started.
SMSRxDialog getting called each time but SaveMessages and ShowMessages are not getting called. either one of them getting started at the same time. Which is secondly called.
If I start SaveMessage and then showMessage then sahow message is getting started but not starting save Message. or vice-Versa..
Is there need to stop first activity before starting another.

Thanks in Advance.

Following is the code:

package screenmagic.myfirstapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MQTTMessageReceiver extends BroadcastReceiver  
{  
private static final String TAG = "MQTTMessageReceiver";
@Override   
public void onReceive(Context context, Intent intent)  
{  
    Bundle notificationData = intent.getExtras();  
    String newTopic = notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_TOPIC);  
    String newData  = notificationData.getString(MQTTService.MQTT_MSG_RECEIVED_MSG);              
    Log.v("REG", "new Topic :" + newTopic);
    Log.v("REG", "new Data: "+ newData);


    Intent chatIntent = new Intent();
    chatIntent.setClass(context, SaveMessages.class);
    context.startActivity(chatIntent);  

    Intent di = new Intent();
    di.setClass(context, SMSRxDialog.class);
    di.putExtra(SMSRxDialog.SMS_FROM_ADDRESS_EXTRA, "MQTT");
    di.putExtra(SMSRxDialog.SMS_FROM_DISPLAY_NAME_EXTRA, newTopic);
    di.putExtra(SMSRxDialog.SMS_MESSAGE_EXTRA, newData);
    di.putExtra(SMSRxDialog.DIALOG_TITLE, "Chat Received");
    context.startActivity(di);  

    Intent msgIntent = new Intent();
    msgIntent.setClass(context, ShowMessages.class);
    context.startActivity(msgIntent);   

    }

}  

I called three activities here, which are:

package screenmagic.myfirstapp;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class ShowMessages extends Activity {
 DatabaseHandler db;
 private static final String TAG = "ShowMessages";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   db = new DatabaseHandler(this);
   Log.i("REGISTER", " Calling showChatMessages");
   showChatMessages();

}

public void showChatMessages(){
      Log.i(TAG, "Message recieved:  showChatMessages" );
        Log.d(TAG, "Getting data from database ..");
        Log.d("Reading: ", "Reading all contacts..");

      List<Messages> message = db.getAllMessages();       

        for (Messages msg : message) {
            String log = "Id: "+msg.getID()+"Topic: "+msg.getTopic()+" ,From Host: " + msg.getFrom() + " ,Message: " + msg.getMessage();
                // Writing Contacts to log
        Log.d("Record: ", log);
        Log.d(TAG, "Data retrieved from database ..");
        }

}
}

another is :

package screenmagic.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SaveMessages extends Activity {
 DatabaseHandler db;
 private static final String TAG = "SaveMessages";

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, " Creating  DatabaseHandler object");
    db = new DatabaseHandler(this);
    Log.i(TAG, "Created  DatabaseHandler object");
    saveChatMessage();

}

public void saveChatMessage(){
      Log.i(TAG, "Message recieved:  saveChatMessage" );
      Log.d("Insert: ", "Inserting ..");
      db.addMessage(new Messages(18,"txtbox.in123", "asmita","test MQTT message from Asmita"));
      Log.d("Insert: ", "Data inserted ..");

}
}

Third Acivity ::

package screenmagic.myfirstapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.speech.tts.TextToSpeech.OnInitListener;
//import android.speech.tts.TextToSpeech;
//import android.speech.tts.TextToSpeech.OnInitListener;


public class SMSRxDialog extends Activity implements OnInitListener {
 private static final String TAG = "SmsReceivedDialog";

    private static final int DIALOG_SHOW_MESSAGE = 1;

    public static final String SMS_FROM_ADDRESS_EXTRA = "com.example.android.apis.os.SMS_FROM_ADDRESS";
    public static final String SMS_FROM_DISPLAY_NAME_EXTRA = "com.example.android.apis.os.SMS_FROM_DISPLAY_NAME";
    public static final String SMS_MESSAGE_EXTRA = "com.example.android.apis.os.SMS_MESSAGE";
    public static final String DIALOG_TITLE = "screenmagic.myfirstapp.DIALOG_TITLE";

   // private TextToSpeech mTts;

    private String mFromDisplayName;
    private String mFromAddress;
    private String mMessage;
    private String mFullBodyString;
    private String mDialogTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDialogTitle = getIntent().getExtras().getString(DIALOG_TITLE);

        mFromAddress = getIntent().getExtras().getString(SMS_FROM_ADDRESS_EXTRA);
        mFromDisplayName = getIntent().getExtras().getString(SMS_FROM_DISPLAY_NAME_EXTRA);
        mMessage = getIntent().getExtras().getString(SMS_MESSAGE_EXTRA);

         mFullBodyString = String.format(
                getResources().getString(R.string.sms_speak_string_format),
                mFromDisplayName,
                mMessage);

        extracted();
        //mTts = new TextToSpeech(this, this);

    }

    private void extracted() {
        showDialog(DIALOG_SHOW_MESSAGE);
    }

    public void onInit(int status) {
    /*if (status == TextToSpeech.SUCCESS) {
            int result = mTts.setLanguage(Locale.US);
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e(TAG, "TTS language is not available.");
            } else {
                mTts.speak(mFullBodyString, TextToSpeech.QUEUE_ADD, null);
            }
        } else {
            // Initialization failed.
            Log.e(TAG, "Could not initialize TTS.");
        }

        */
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_SHOW_MESSAGE:
            return new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_email)
                    .setTitle(mDialogTitle)
                    .setMessage(mFullBodyString)
                    .setNegativeButton(R.string.dismiss, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.dismiss();
                            finish();
                        }
                    })
                    .setOnCancelListener(new DialogInterface.OnCancelListener() {
                        public void onCancel(DialogInterface dialog) {
                            finish();
                        }
                    }).create();
        }
        return null;
    }

}

Database code is ::

package screenmagic.myfirstapp;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "SMSMAGIC2";

// Contacts table name
private static final String TABLE_NAME = "chatmessages";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_FROM = "fromHost";
private static final String KEY_TOPIC = "topic";
private static final String KEY_MESSAGE = "message";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_FROM + " TEXT,"
            + KEY_TOPIC + " TEXT," +  KEY_MESSAGE + " TEXT )";
    Log.d("Insert: ", "Table create query:: "+CREATE_CONTACTS_TABLE);

    db.execSQL(CREATE_CONTACTS_TABLE);
    Log.d("Insert: ", "Table created ..");

}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    Log.d("Upgrade: ", "Called onUpgrade function"+TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    Log.d("Insert: ", "Dropped Table created .."+TABLE_NAME);
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new contact
void addMessage(Messages message) {

    Log.d("Insert: ", "addMessage function  ..");

    ContentValues values = new ContentValues();

    values.put(KEY_MESSAGE, message.getMessage()); // message 
    values.put(KEY_TOPIC, message.getTopic()); // topic Name
    values.put(KEY_FROM, message.getFrom()); // from
    values.put(KEY_ID, message.getID()); //Id 

    Log.d("Insert: ", "Message :"+ message.getMessage());
    Log.d("Insert: ", "topic :"+ message.getTopic());
    Log.d("Insert: ", "from :"+ message.getFrom());
    Log.d("Insert: ", "id :"+ message.getID());

    Log.d("Insert: ", "values added to ContentValues.");
    SQLiteDatabase db = this.getWritableDatabase();
    Log.i("Insert: ", "Object Created from SQLiteDatabase.");

    try {
            db.insert(TABLE_NAME, null, values);
            Log.d("Insert: ", "Inserted Row..");
            db.close();
            Log.d("Insert: ", "Closing database connection.");
      } catch (Exception e) {
//           /   Toast.makeText(this, "misfunctioning open" + e.toString(),Toast.LENGTH_LONG).show();
            Log.d("Insert: ", "Exception Row.."+e.getMessage());

        }

}

// Getting All Contacts
public List<Messages> getAllMessages() {
    List<Messages> msgList = new ArrayList<Messages>();

    String selectQuery = "SELECT  * FROM " + TABLE_NAME;
    Log.d("Query :: ", "selectQuery ::"+selectQuery);
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Messages message = new Messages();
            message.setID(Integer.parseInt(cursor.getString(0)));
            message.setTopic(cursor.getString(1));
            message.setFrom(cursor.getString(2));
            message.setMessage(cursor.getString(3));
            msgList.add(message);
        } while (cursor.moveToNext());
    }
    return msgList;
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:44:10+00:00Added an answer on June 15, 2026 at 11:44 am

    no point starting 3 activities from reciever as you dont know what the behaviour would be.

    Always do A -> B -> C -> D rather than directly A-> B,C,D

    where A is your Reciever and B,C,D are your activities

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a .NET Web Service and I want to run multiple
I really love node.js but it´s really complicating when you want to run multiple
I want to run multiple instances of apache on one single machine? How to
I want to run an update query. The query will be run against multiple
I want to write a method that run multiple threads and I want before
I want to run multiple Python versions in my box. Is there any version
I want to run multiple versions of MATLAB (with standalone licenses) on a Windows
I have a situation where I want to run multiple EventMachines in Ruby -
I am doing a distributed systems project & I want to run multiple servers
I want to run multiple instances of the following script parallel: for($i = 0;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.