Java 在android studio中打开应用程序之前如何获取通知
如何在打开应用程序之前或在我关闭应用程序时获取通知移动设备上仍显示通知。或者,当我启动手机时,我希望收到通知,但应用程序不应运行Java 在android studio中打开应用程序之前如何获取通知,java,android,Java,Android,如何在打开应用程序之前或在我关闭应用程序时获取通知移动设备上仍显示通知。或者,当我启动手机时,我希望收到通知,但应用程序不应运行 public class BeaconService extends Service { @Override public IBinder onBind(Intent arg0) { return null; } @Override public int onStartCommand(Intent intent,
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>
如果用户不第一次打开应用程序,只需安装应用程序就无法直接启动服务 您只需查看这两个链接了解更多信息 (一) (二) 若要发出当前通知,请添加此行:
builder.setOngoing(true)
希望这将有助于您@saeed notification display,但在打开应用程序之前需要。@saeed我的实际问题是我需要在后台显示通知服务,即当我启动手机时,然后自动显示通知。您能测试show notification()吗;是不是在打电话。。仅仅Log@saeed显示showNotification();正在呼叫,但当我停止或终止应用程序时,显示通知未呼叫