5

मेरी सूचना एक सेवा (service.java) और क्या सेवा कर बैटरी स्तर की एक अधिसूचना शुरू जब वरीयता स्क्रीन में एक checkboxpreference चेक किया गया है .. क्या अब नहीं काम करता है आशय है अधिसूचना पर क्लिक करने के लिए MainActivity में प्रवेश करने के लिए। यह कोडअधिसूचना में आशय से काम नहीं करता

if(mprefs.getBoolean("notification_a", false)==true){ 
    notificationBuilder = new NotificationCompat.Builder(context); 

    notificationBuilder.setOngoing(true); 
    notificationBuilder.setContentTitle("Battery Stats Informations"); 
    notificationBuilder.setContentText("Carica residua: " +level+"%" + " " + "Temperatura: " +temperature+ "°C"); 
    //notificationBuilder.setTicker("Informazioni batteria"); 
    notificationBuilder.setWhen(System.currentTimeMillis()); 
    notificationBuilder.setSmallIcon(R.drawable.icon_small_not); 
    Intent notificationIntent = new Intent(context, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0); 
    notificationBuilder.setContentIntent(contentIntent); 

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    Notification not=notificationBuilder.build(); 

    mNotificationManager.notify(SIMPLE_NOTIFICATION_ID,not); 
} else { 
    mNotificationManager.cancelAll(); 
} 

इरादा Intent notificationIntent = new Intent(context, MainActivity.class कार्य नहीं करता है। कोई मदद करता है?

उत्तर

5

आप गतिविधि शुरू करने के लिए जब अधिसूचना क्लिक किया जाता है इरादा है: उपयोग:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

एक PendingIntent है कि एक नई गतिविधि शुरू कर देंगे, बुला Context.startActivity (आशय) की तरह प्राप्त करें।

के स्थान पर:

PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0); 

एक PendingIntent कि एक प्रसारण प्रदर्शन करेंगे, Context.sendBroadcast बुला तरह पुनः प्राप्त()।

+0

ठीक है, तो मैं कोशिश करूँगा .. लेकिन जो 'getActivity' और' getBroadcast' के बीच अलग है? –

संबंधित मुद्दे