Hi There,

The specific problem is how to create a proper notification icon for an 
Android App out of a Broadcast Receiver "Receive"-method and keep the 
original intent, so that the user can continue the minimized app by 
clicking on the notification icon.

In my case, the notification icon appears, but - if the app is minimized - 
a new instance of the app is created which crashes because variables are 
not initialized. After the crash, the previous instance shows up. So how 
can I fix this?

Code:

public class Receiver extends BroadcastReceiver  
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {      
                  
      //prepare notification
      NotificationCompat.Builder notification_builder_ = new 
NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon_xhdpi_96).setContentTitle("test").setContentText("Notification!");
 
        
//notification_builder_.setContentIntent(PendingIntent.getActivity(context, 0, 
new Intent(context, 
Activity_Class.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0));


        
notification_builder_.setContentIntent(PendingIntent.getActivity(context, 0, 
new Intent(context, 
Activity_Class.class).addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT), 0));
        
        //show notification
       Notification note = notification_builder_.build(); note.defaults = 
Notification.DEFAULT_ALL; 
       
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(0,
 note);
    }
}

Thank you,


On Thursday, January 21, 2016 at 4:30:55 AM UTC+1, TreKing wrote:
>
>
> On Fri, Jan 15, 2016 at 4:29 AM, Florian Bock <florian.b...@gmail.com 
> <javascript:>> wrote:
>
>> - Actual behavior: The typical "the App has crashed" message appears. 
>> When I close it, my app is resumed at the point where it was minimized. If 
>> I debug, I recognize that a second instance of my app is created where the 
>> required variables are not set and therefore it crashes. Nevertheless, my 
>> old instance is still present and resumed afterwards... How can I fix this? 
>> What is the correct code to create a notification in a Broadcast Receiver 
>> (I am still confused with the context/intent/pending intent things)? 
>
>
> Please post some code illustrating how you're launching your Activity and 
> setting "required variables" in both cases.
>
>
> -------------------------------------------------------------------------------------------------
> TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago 
> transit tracking app for Android-powered devices
>

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/cb3f0d12-393c-4de9-b43a-60984a14a470%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to