2013-12-09 2 views
-2

Я хочу показать пользователю уведомление о том, работает ли моя аппликация или нет. Это код уведомления, который я пытался реализовать. Уведомление отображается только при запуске приложения. Но я хочу отображать это уведомление, даже когда мое приложение не работает. Как я могу это достичь?отображает уведомление о запуске приложения или в фоновом режиме

public void Notification(String s) 
{ 
     String ns = Context.NOTIFICATION_SERVICE; 

     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.notification_icon; 
     CharSequence tickerText = "Ready for Play time."; // ticker-text 
     long when = System.currentTimeMillis(); 
     Context context = getApplicationContext(); 
     CharSequence contentTitle = "Play Time"; 
     CharSequence contentText = "Your match is at "+s; 
     Intent notificationIntent = new Intent(this,ScheldueNotification .class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     Notification notification = new Notification(icon, tickerText, when); 
     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     // and this 

     mNotificationManager.notify(MY_NOTIFICATION_ID, notification); 

     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.vibrate = new long[] { 0, 100, 200, 300 }; 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

} 
+0

Вы хотите показать уведомление, когда ваше приложение находится в фоновом режиме, не так ли? –

+0

Нет. Мое приложение еще не работает. но хочу показать уведомление. – vcav

+0

Для этого вам придется использовать 'service'. – zanky

ответ

0

Вам нужно создать WakefulBroadcastReceiver для частичной блокировки бодрствования, если вы не делаете это, добавьте это к вашему Проект-

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Explicitly specify that GcmIntentService will handle the intent. 
     ComponentName comp = new ComponentName(context.getPackageName(), 
       GcmIntentService.class.getName()); 
     // Start the service, keeping the device awake while it is launching. 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 
0

Создать и вызывать эту функцию в другом классе, который будет распространяться Service классом , После этого запуска службы от вашей деятельности, которая будет посылать notification.Try этот код:

public class MyService extends Service { 

    NotificationManager manager; 

    @Override 
    public void onCreate() { 
    } 

    @Override 
    public IBinder onBind(Intent intent) {   
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 
     Notification(String s); 
    } 

    public void Notification(String s) 
    { 
     String ns = Context.NOTIFICATION_SERVICE; 

     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.notification_icon; 
     CharSequence tickerText = "Ready for Play time."; 
     long when = System.currentTimeMillis(); 
     Context context = getApplicationContext(); 
     CharSequence contentTitle = "Play Time"; 
     CharSequence contentText = "Your match is at "+s; 
     Intent notificationIntent = new Intent(this,ScheldueNotification .class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     Notification notification = new Notification(icon, tickerText, when); 
     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 


     mNotificationManager.notify(MY_NOTIFICATION_ID, notification); 

     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.vibrate = new long[] { 0, 100, 200, 300 }; 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

} 

Написать этот код в вашей деятельности, которая будет вызывать службу.

private Intent myIntent; 
myIntent = new Intent(myActivity.this,MyService.class); 
startService(myIntent); 
Смежные вопросы