2014-06-05 2 views
0

알림을 통해 내 활동을 열면 내 활동에 무언가를하려고합니다. 알림을 통해 활동을 여는 경우 무언가 수행

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

답변

0

당신은에 약간의 호환 문제가 당신의 암호. 당신은 생성하고 여분의 번들이지만 부울은 여분의 번들에 할당하지 않습니다.

Bundle extras = intent.getExtras(); 
Intent startServiceIntent = new Intent(context, MainActivity.class); 
startServiceIntent.putExtra("fromNotification", true); 
startServiceIntent.putExtras(extras); 
context.startService(startServiceIntent); 

변경 것과 :

Intent startServiceIntent = new Intent(context, MainActivity.class); 
Bundle extras = startServiceIntent.getExtras(); 
extras.putBoolean("fromNotification", true); 
startServiceIntent.putExtras(extras); 
context.startService(startServiceIntent); 

업데이트 : 개인적으로

난 그냥 내가 필요보다() 메서드를 intent.putExtra를 사용하지만, 번들 방법을 사용하지 않는

boolean fromNotification = getIntent().getBooleanExtra("fromNotification", false); 
if (fromNoticiation) { 
    // I was called by notification 
} else { 
    // started from somewhere else 
} 
+0

이 줄에서 NullPointerException이 표시됩니다. 'extras.putBoolean ("notNotification", true);'나는 t를 가지지 않습니다. o 번들을 사용하십시오. 'intent.putExtra()'를 사용하여 다른 것을 변경해야합니까? – Geoff

+0

intent.putExtra() 변형을 사용하고 번들 항목을 건너 뜁니다. 부울을 가져 오는 데 필요한 코드는 이미 내 대답의 일부입니다 (업데이트 된 부분 참조). nullPointer는 아마도 startServiceIntent가 아직 추가로 저장된 번들이 아니기 때문일 수 있습니다. 그래서 당신은 단지'Bundle extras = new Bundle()'을 만들 수 있습니다. – WarrenFaith

+0

intent.putExtra() 변형을 사용하여 더 이상 NullPointerExcpetion을 가져 오지 못하지만 여전히 false로 실행됩니다. 내가 가진 것을 보여주기 위해 원래 게시물을 업데이트했습니다. – Geoff

0
이됩니다 방송 수신기를 시작한다 그렇게 할 때 당신은 분명히 (의도로 작업을 설정하고 그것을 위해 수신기를 등록 할 수 있습니다

) 해당 메시지를 수신하고 올바른지 확인하려면 다음과 같이 확인할 수 있습니다.

if(receivedIntent.getAction().equals("yourAction"))//do sth 

그리고 당신의 의도를 사용하려면이 코드 작업을 추가합니다 : 사용자의 많은 문제 열어 알림을 경험하고 있기 때문에 안드로이드 4.3

PendingIntent myIntent = PendingIntent.getActivity(context, 1000, new Intent(context, MainActivity.class).setAction("yourAction"), 0); 

또한 요청 코드 대신 0 1000을 사용하고자합니다. .. 그게 그걸 고쳐 줘.

당신이 얻을 엑스트라가 null이고, 당신이 의도를 잘못 보내 코드 ... 사용이 때문이기 때문에 귀하의 else 문은 항상 화재 :

Intent startServiceIntent = new Intent(context, MainActivity.class); 
startServiceIntent.putExtra("fromNotification", true); 
context.startService(startServiceIntent); 
관련 문제