Мое оригинальное приложение получает текст и использует разделение символов для разделения и отображения в текстовых изображениях. Затем я реализовал коды, чтобы вызывать активность из BroadcastReceiver, поэтому действие выводится на передний план при получении текстов, но текстовые просмотры приложения не обновляются.'' Launch MainActivity '' предотвращает отображение текстов
Если я удаляю из SMSReceiver.java, он работает нормально, но приложение не выводится на передний план.
//--Launch the MainActivity--//
Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivityIntent);
код SMSReceiver.java:
package com.example.smssend;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//--Get the SMS Message passed in---//
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//--Retrieve the SMS message received--//
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += msgs[i].getMessageBody().toString();
}
//--Display the new SMS Message--//
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
String sender = null;
//--Launch the MainActivity--//
Intent mainActivityIntent = new Intent(context, MainActivity.class);
mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainActivityIntent);
//--Send a broadcast intent to update the SMS received in the activity--//
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
}
Я добавил свой onResume() часть к моему MainActivity.java: Тем не менее, текст пока не отображается. @Override \t protected void onResume() { \t \t super.onResume(); \t \t Intent myIntent = getIntent(); \t \t Строка sms = myIntent.getStringExtra ("sms"); \t \t если (смс = NULL && "" равна (смс)!). { TextView mTextView = NULL; \t \t \t \t \t mTextView.setText (смс); } \t // - Зарегистрировать приемник - // \t registerReceiver (intentReceiver, intentFilter); \t super.onResume(); } – Joshua
Можете ли вы выслать полный код своей деятельности? – jDur