2016-03-03 1 views
0

Как получить уведомление перед открытием приложения или когда я убью приложение, показывая уведомление на мобильном устройстве. или когда я запускаю телефон, я хочу получать уведомление, но приложение не должно работать.Как получить уведомление перед открытым приложением в студии android

public class BeaconService extends Service { 
    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     showNotification(); 
     return START_STICKY; 
    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 
    } 
    private void showNotification() { 
     NotificationCompat.Builder mBuilder = 
       (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
         .setSmallIcon(R.drawable.ic_loc) 
         .setContentTitle("Welcome to Brillio") 
         .setContentText("Hello Mansur, Welcome to Brillio.") 
         .setPriority(2) 
         .setOnlyAlertOnce(false); 
     Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     mBuilder.setSound(alarmSound); 
     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(2001, mBuilder.build()); 
    } 
} 

BeaconReceiver.java

public class BeaconReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Intent intentLunch = new Intent(context, BeaconService.class); 
     context.startService(intentLunch); 

    } 
} 

menifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<service 
     android:name=".BeaconService"/> 
     <receiver android:name=".BeaconReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
       <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
      </intent-filter> 
     </receiver> 
+0

@saeed оповещение но хотите, прежде чем открывать приложение. – Alam

+0

@saeed Моя фактическая проблема заключается в том, что уведомление об уведомлении в фоновом режиме означает, что когда я запускаю свой мобильный телефон, уведомление автоматически открывается. – Alam

+0

Можете ли вы проверить шоу showNotification(); звонит или нет .. только Log – saeed

ответ

0

Там нет простого способа запуска службы на просто установить приложение, не открывая пользователь это первый раз.

вы просто проверить эти две ссылки для более

1) How to start a Service when .apk is Installed for the first time

2) How to start android service on installation

Чтобы presistant уведомления, добавьте следующую строку:

builder.setOngoing(true) 

Надеется, что это помогает вы

Смежные вопросы