2

フラグメントでのフォアグラウンド サービスの開始について質問があります。

Intent intent = new Intent(this, MainActivity.class);  
PendingIntent pendingIndent = PendingIntent.getActivity(this, 1, intent, 0);  
Notification mNotification = new Notification(R.drawable.ic_music, msg, System.currentTimeMillis());  
mNotification.setLatestEventInfo(this, title, msg, pendingIndent);  
mNotification.flags = mNotification.flags|Notification.FLAG_ONGOING_EVENT;  
startForeground(1000, mNotification);

↑ このコードがアクティビティでフォアグラウンド サービスを開始することはわかっています。

だから私はフラグメントで使用するためにいくつかのコードを変更しました

Intent intent = new Intent(getActivity(), xxx.class);  
PendingIntent pendingIndent = PendingIntent.getActivity(getActivity(), 1, intent, 0);  
Notification noti = new Notification(R.drawable.ic_xxx, "xx", System.currentTimeMillis());  
noti.setLatestEventInfo(getActivity(), "title", "xx", pendingIndent);  
noti.flags = noti.flags|Notification.FLAG_ONGOING_EVENT;  
getActivity().startForeground(1000, noti); 

そして、私はこれに問題がありました:

getActivity().startForeground(1000, noti);

getActivity() には startForeground メソッドがなく、フラグメントでフォアグラウンド サービスを開始したい。

私に何ができる?

4

1 に答える 1

0

onStartCommand()あなたのサービスの方法でこれを試してください:

startForeground(NOTIFICATION_ID, new Notification());

START_STICKYを呼び出すまでサービスを実行し続けるaを返す必要がある場合がありますselfStop()

乾杯!

于 2013-03-20T07:52:04.757 に答える