0
我想要在啓動或啓動android mobile時顯示消息或通知。通知顯示,但是當我銷燬應用程序時,它不顯示消息或通知。所以請任何人幫助我。我想要在啓動或啓動android mobile時顯示消息或通知。
public class BeaconService extends Service {
Handler mHandler = new Handler();
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10000);
mHandler.post(new Runnable() {
@Override
public void run() {
showNotification();
}
});
}catch (Exception e){
}
}
}
}).start();
return START_STICKY;
}
private void showNotification() {
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_loc)
.setContentTitle("Welcome to Ford Show Room")
.setContentText("Hello Puneet, Welcome to Ford! You'll be shortly attended by Karthik! ")
.setPriority(2)
.setOnlyAlertOnce(true);
Intent resultIntent = new Intent(this, SlideMenuActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SlideMenuActivity.class);
stackBuilder.addNextIntent(resultIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2001, mBuilder.build());
}
}
BeaconReceiverService.java
public class BeaconReceiverService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("DEBUG", "Creating the intent");
Intent service = new Intent(context, BeaconService.class);
context.startService(service);
}
}
服務在menifest.xml
<service
android:name=".BeaconService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</service>
<receiver android:name=".BeaconReceiverService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>