我是android新手。任何人都可以發佈「有通知的android前臺服務示例」的鏈接或代碼。我搜索了一下,但沒有得到任何前臺服務的例子。前臺服務示例
Q
前臺服務示例
8
A
回答
15
創建Notification
,可能使用Notification.Builder
或NotificationCompat.Builder
,並傳遞到startForeground()
上的服務:
public class Downloader extends IntentService {
private static int FOREGROUND_ID=1338;
public Downloader() {
super("Downloader");
}
@Override
public void onHandleIntent(Intent i) {
startForeground(FOREGROUND_ID,
buildForegroundNotification(filename));
// do useful work here
stopForeground(true);
}
private Notification buildForegroundNotification(String filename) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setOngoing(true);
b.setContentTitle(getString(R.string.downloading))
.setContentText(filename)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setTicker(getString(R.string.downloading));
return(b.build());
}
}
(從this sample project修剪牀服務)
3
一個更好的例子將是:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Start Foreground Intent ");
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Intent previousIntent = new Intent(this, ForegroundService.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
previousIntent, 0);
Intent playIntent = new Intent(this, ForegroundService.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
playIntent, 0);
Intent nextIntent = new Intent(this, ForegroundService.class);
nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
PendingIntent pnextIntent = PendingIntent.getService(this, 0,
nextIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.truiton_short);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Truiton Music Player")
.setTicker("Truiton Music Player")
.setContentText("My Music")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(
Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_media_previous,
"Previous", ppreviousIntent)
.addAction(android.R.drawable.ic_media_play, "Play",
pplayIntent)
.addAction(android.R.drawable.ic_media_next, "Next",
pnextIntent).build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
notification);
} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
Log.i(LOG_TAG, "Clicked Previous");
} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
Log.i(LOG_TAG, "Clicked Play");
} else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
Log.i(LOG_TAG, "Clicked Next");
} else if (intent.getAction().equals(
Constants.ACTION.STOPFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Stop Foreground Intent");
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
參考了tutorial here ..可能對您有幫助
相關問題
- 1. 未顯示Android前臺服務通知
- 2. Android InstantApp:前臺服務
- 3. 取消前臺服務的通知是否將服務拉離前臺?
- 4. 一旦服務不在前臺,可以從前臺服務中取消通知
- 5. 停止例程服務開始將現有後臺進程帶到前臺
- 6. php網絡服務示例
- 7. TypeScript語言服務示例
- 8. 示例Web服務 - 更新
- 9. 使用示例服務
- 10. PUT服務方法示例?
- 11. 示例數據服務
- 12. FinalBuilder服務器+ FastMM4示例
- 13. Angularjs資源服務示例
- 14. MVVM數據服務示例
- 15. Zend Soap服務器示例
- 16. dart聊天服務器示例,從另一臺機器連接
- 17. 在一臺服務器上實現簡單的SAML php示例
- 18. 獲取後臺服務實例
- 19. 前臺服務,報警經理,notificationsnow顯示
- 20. 前臺服務通知未顯示在Android 8上。+
- 21. Products.SQLAlchemyPAS作爲前臺而不是服務
- 22. 在前臺運行Windows服務
- 23. 可綁定的Android前臺服務
- 24. 前臺服務相關的問題android
- 25. 打盹模式暫停前臺服務
- 26. 如何暫停前臺服務
- 27. 綁定的服務總是前臺?
- 28. 前臺服務保留內存對象
- 29. 打盹模式和前臺服務
- 30. Ubuntu在前臺運行服務