알림을 통해 내 활동을 열면 내 활동에 무언가를하려고합니다. 알림을 통해 활동을 여는 경우 무언가 수행
Bundle extras = intent.getExtras();
Intent startServiceIntent = new Intent(context, MainActivity.class);
startServiceIntent.putExtra("fromNotification", true);
startServiceIntent.putExtras(extras);
context.startService(startServiceIntent);
...
PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
그리고 내 주요 활동에
, 내가 전화하려고 : 내 브로드 캐스트 리시버에서, 나는 이것이이Bundle extras = getIntent().getExtras();
if(extras != null) {
if(extras.getBoolean("fromNotification")) {
Toast.makeText(this, "from notification", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "extras = " + extras, Toast.LENGTH_LONG).show();
}
문제 extras
가 null 말하면서 else
문은 항상 발광 것입니다.
무엇이 여기에 있습니까?
UPDATE :
BroadcastReceiver
Intent startServiceIntent = new Intent(context, MainActivity.class);
startServiceIntent.putExtra("fromNotification", true);
context.startService(startServiceIntent);
MainActivity
boolean fromNotification = getIntent().getBooleanExtra("fromNotification", false);
if (fromNotification) {
Toast.makeText(this, "from notification", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, fromNotification + " elsewhere", Toast.LENGTH_LONG).show();
}
이 줄에서 NullPointerException이 표시됩니다. 'extras.putBoolean ("notNotification", true);'나는 t를 가지지 않습니다. o 번들을 사용하십시오. 'intent.putExtra()'를 사용하여 다른 것을 변경해야합니까? – Geoff
intent.putExtra() 변형을 사용하고 번들 항목을 건너 뜁니다. 부울을 가져 오는 데 필요한 코드는 이미 내 대답의 일부입니다 (업데이트 된 부분 참조). nullPointer는 아마도 startServiceIntent가 아직 추가로 저장된 번들이 아니기 때문일 수 있습니다. 그래서 당신은 단지'Bundle extras = new Bundle()'을 만들 수 있습니다. – WarrenFaith
intent.putExtra() 변형을 사용하여 더 이상 NullPointerExcpetion을 가져 오지 못하지만 여전히 false로 실행됩니다. 내가 가진 것을 보여주기 위해 원래 게시물을 업데이트했습니다. – Geoff