SMS 텍스트를 수신하는이 응용 프로그램을 만들고 싶으면 알림을 표시하고 사용자가 클릭하면 새 활동이 팝업됩니다 SMS 메시지와 함께. 그래서 SMS 텍스트를 수신 할 때 작동하는 BroadcastReceiver 클래스와 같은 기능을 수행하면 모든 기능이 잘 작동합니다. 모든 알림을 멋지게 표시합니다.알림을받을 때 새로운 활동을 얻는 데 문제가 발생했습니다.
public class SMSReceiver extends BroadcastReceiver {
static final int notificationId = 1;
@Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
newNotification(context, phoneNumber, message);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void newNotification(Context context, String title, String text) {
NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context, NotificationActivity.class);
notifyIntent.putExtra("SMSText", text);
notifyIntent.setAction(text);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notifyIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentIntent(contentIntent)
.setContentText(text)
.setContentTitle(title)
.setTicker(text)
.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notifyMgr.notify(notificationId, notification);
}
}
유일한 문제는 알림을 클릭했을 때입니다. 처음 완벽하게 작동하면 NotificationActivity 클래스가 SMS 메시지를 표시합니다. 그러나 그 후에는 정보가 업데이트되지 않습니다. 나는 앱이 이미 NotificationActivity가 열려 있다는 것을 깨닫고 다른 앱을 열지 않는다고 가정합니다. 새 알림을 만들 때 플래그 FLAG_ACTIVITY_NEW_TASK
을 올바르게 추가하더라도. 또한 여기 내 NotificationActivity 클래스
public class NotificationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
newString = null;
} else {
newString = extras.getString("SMSText");
}
} else {
newString = (String) savedInstanceState.getSerializable("SMSText");
}
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(newString);
}
}
나는 또한 런처 활동, MainActivity
을 할 수 있지만 않습니다 모두가 SMSReceiver를 등록하기 때문에 보여줄 필요 느꼈다. 어떤 도움이라도 대단히 감사하겠습니다.
내 업데이트 대답 – mehul