gpt4 book ai didi

android - 如何保持应用程序在后台运行?

转载 作者:行者123 更新时间:2023-11-29 01:43:57 28 4
gpt4 key购买 nike

我正在制作一个短信阅读器应用程序。一旦他们在广播监听器的帮助下收到传入的消息我的 Activity 收到消息,并在文本到语音类的帮助下读取消息。问题是一旦他们收到消息,我的 Activity 就会出现在前台。任何人都可以帮助我如何让我的应用程序仅在后台运行吗?我正在粘贴我的代码以供引用。谢谢 !

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");
}

}
}

最佳答案

您正在调用文本到语音 Activity ,这就是它每次弹出的原因。如果您想将其保留在后台,请将您的文字转语音 Activity 更改为服务。

关于android - 如何保持应用程序在后台运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22643505/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com