最近我们突然看到了下面的一些堆栈跟踪.为什么会这样?这是应用程序尝试通过媒体通知和所有内容将音频评论服务移动到前台时.
java.lang.SecurityException: Permission Denial: startForeground from pid=1824, uid=10479 requires android.permission.FOREGROUND_SERVICE
at android.os.Parcel.createException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1910)
at android.os.Parcel.readException(Parcel.java:1860)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5198)
at android.app.Service.startForeground(Service.java:695)
at com.example.app.services.AudioService.setUpMediaNotification(AudioService.java:372)
at com.example.app.services.AudioService.setUpAndStartAudioFeed(AudioService.java:328)
at com.example.app.services.AudioService.onStartCommand(AudioService.java:228)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3667)
at android.app.ActivityThread.access$1600(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.enforcePermission(ActivityManagerService.java:9186)
at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:1189)
at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:870)
at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:20434)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:976)
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个用于监控传入SMS消息的应用程序,并通过传入SMS启动程序,它也应该从SMS读取内容.
工作流程:
Service,Async Task&Thread之间有什么区别.如果我没有错,他们都习惯在后台做一些事情.那么,如何决定使用哪个以及何时使用?
安卓:
public class LocationService extends Service {
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
startActivity(new Intent(this, activity.class));
}
}
Run Code Online (Sandbox Code Playgroud)
我推出了这项服务 Activity
在Activity如果条件满足启动
startService(new Intent(WozzonActivity.this, LocationService.class));
Run Code Online (Sandbox Code Playgroud)
从我LocationService上面提到的无法启动Activity,我怎样才能获得当前Activity在服务类中运行的上下文?
我创建了一个由其他应用程序通过AIDL绑定的服务,并将其添加到清单中,如下所示:
<service android:name=".MyService">
<intent-filter>
<action android:name="org.example.android.myservicedemo.IService" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
其中IService是AIDL接口.
通过这种方式,Eclipse向我显示警告导出的服务不需要权限.如果我删除intent-filter,警告消失,但显然应用程序无法绑定到服务.
这个警告意味着什么?
你能帮我理解一下IntentServicea和a 之间的区别Service吗?
所以我不确定在何处/如何实现此方法以使我的服务在前台运行.目前我在另一项活动中通过以下方式开始我的服务:
Intent i = new Intent(context, myService.class);
context.startService(i);
Run Code Online (Sandbox Code Playgroud)
然后在myServices的onCreate()中我尝试startForeground()......?
Notification notification = new Notification();
startForeground(1, notification);
Run Code Online (Sandbox Code Playgroud)
所以是的,我有点失落,不确定如何实现这一点.
我MainActicity 开始RefreshService用Intent具有boolean所谓的多余isNextWeek.
我RefreshService做了一个在用户点击它时Notification启动我MainActivity的.
这看起来像这样:
Log.d("Refresh", "RefreshService got: isNextWeek: " + String.valueOf(isNextWeek));
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra(MainActivity.IS_NEXT_WEEK, isNextWeek);
Log.d("Refresh", "RefreshService put in Intent: isNextWeek: " + String.valueOf(notificationIntent.getBooleanExtra(MainActivity.IS_NEXT_WEEK,false)));
pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
builder = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText("ContentText").setSmallIcon(R.drawable.ic_notification).setContentIntent(pendingIntent);
notification = builder.build();
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_REFRESH, notification);
Run Code Online (Sandbox Code Playgroud)
你可以看到notificationIntent应该有boolean额外IS_NEXT_WEEK的值,isNextWeek其中放入了PendingIntent.
当我现在点击这个时, …
android android-intent android-service android-pendingintent
我希望有一个在后台运行的应用程序,它知道任何内置应用程序(消息,联系人等)何时运行.
所以我的问题是:
我应该如何在后台运行我的应用程序.
我的后台应用程序如何知道前台运行的应用程序是什么.
有经验的人的回应将不胜感激.
onStartCommand(Intent, int, int)除了系统通过诸如START_STICKY?之类的标志重新启动服务之外,传递给它的Intent是否还有其他原因是NULL ?
此外,当系统重新启动服务时,该Intent.getAction()方法有时返回NULL ... Intent不仅仅是NULLgetAction()
我也在这里问过,但还没有得到答案.
UPDATE:马克·墨菲聊天后,他建议我返回START_REDELIVER_INTENT的onStartCommand()回调在我的服务,而不是START_STICKY使整个意图在重新启动发送.
我最初没有这样做,因为我担心如果服务试图做某事,那么在服务重新启动的那个中间......它会认识到它开始做那件事吗?我想这是逻辑我需要负责:)