简体   繁体   中英

How to keep app running in background?

I am making an sms reader app .As soon as their is an incoming msg with the help of brodcast listener my activity receives the message and with the help of text to speech class it reads the msg. Problem is as soon as their is an incoming msg, my activity comes in foreground.Can anyone help me how to make my app run in background only ? I am pasting my code for reference. Thanks !

MyBroadcastReceiver. java
package com.example.sms_reader;

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


public class MyBrodcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent intent) 
    {
        Log.d("tag1", "Receiver is activated");
        Log.d("tag2", "msg");
        Bundle bundle = intent.getExtras(); 

        if (bundle != null)
        {   
            Object[] pdus = (Object[]) bundle.get("pdus");
            SmsMessage[] messages = new SmsMessage[pdus.length]; 
            for (int i = 0; i < pdus.length; i++) 
            {
        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
            }
        for (SmsMessage message : messages) 
            {
                String msg = message.getMessageBody();
                long when = message.getTimestampMillis();  
                String from = message.getOriginatingAddress();  
                Log.d("tag2", msg);
                Log.d("tag3", from);
                Intent i = new Intent(arg0 ,Texttospeech.class);
                i.putExtra("msg", msg);
                i.putExtra("when", when);
                i.putExtra("from", from);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                arg0.startActivity(i); 
                Log.d("tag4", "intent called");

            }


        }
        else{
            Log.d("tag2", "bundle not received");
        }
    }

}


Texttospeech.java
package com.example.sms_reader;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class Texttospeech extends Activity implements
TextToSpeech.OnInitListener
{

TextToSpeech tts ; 
String content;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("tag4", "INTENT RECEIVED");
    tts = new TextToSpeech(this, this);    
    Intent data = getIntent();
    Log.d("tag5", "INTENT ");
    String msg = data.getStringExtra("msg");
    String from = data.getStringExtra("from");
    content = "You have received a message from" +from +"and it says"+msg;
    Log.d("tag6", content);
    speakOut(); 

};

private void speakOut() {


    tts.speak(content, TextToSpeech.QUEUE_FLUSH, null);
}   

@Override
public void onDestroy() {
    // Don't forget to shutdown!
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

@Override
public void onInit(int status) {
    // TODO Auto-generated method stub

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.US);

        // tts.setPitch(5); // set pitch level

        // tts.setSpeechRate(2); // set speech speed rate

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } 
        else {

            speakOut();
        }

                                         } 
    else 
    {
        Log.e("TTS", "Initilization Failed");
    }

}
}

You are calling the text to speech activity that's why it pops up every time. If you want to keep it in background change your text to speech activity to service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM