我想分享GCM通知项目.共享按钮响应点击事件,项目也被共享.这里唯一的问题是,通知托盘下方有Intent选择器对话框.用户必须手动关闭状态栏,然后选择要共享的应用程序.我想以编程方式关闭状态栏,以便当用户单击共享时,它会直接向他显示选择应用程序的对话框.
我发现该status bar服务可用于打开/关闭服务.但它仅限于系统应用程序.
private void closeNotificationTray() {
Object service = mContext.getSystemService(Context.STATUS_BAR_SERVICE);
Method collapse;
try {
Class<?> statusBarMngr = Class.forName("android.app.StatusBarManager");
if (Build.VERSION.SDK_INT >= 17)
collapse = statusBarMngr.getMethod("collapsePanels");
else
collapse = statusBarMngr.getMethod("collapse");
collapse.invoke(service);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我用了上面的代码.但我得到"STATUS_BAR_SERVICE无法重新发送"错误.当我在清单中添加以下权限时:
<uses-permission
android:name="android.permission.STATUS_BAR" />
Run Code Online (Sandbox Code Playgroud)
我得到了,只允许系统apps.It不允许我在我的应用程序中使用.有没有办法使用status bar服务或任何其他替代方案?
更新:
我用2行代码解决了上述问题.无需调用STATUS_BAR_SERVICE.
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
mContext.sendBroadcast(it);
Run Code Online (Sandbox Code Playgroud)
调用此意图将自动关闭通知
到目前为止查看了很多类似的问题,但最终没有一个解决方案。
我正在尝试启动前台服务来处理短信。该服务在应用程序首次打开时启动,并通过 kotlin 协程在后台线程上工作。即使应用程序已关闭,此进程仍保持活动状态。
我不太熟悉各种上下文,但我认为 applicationContext 将在应用程序的生命周期中存在,但我为获取 SmsManager 所做的调用返回 null,因此无论应用程序是否打开都是无用的。
主要活动中调用服务:
val textServiceIntent = Intent(this@MainActivity, TextManager::class.java)
applicationContext.startForegroundService(textServiceIntent)
Run Code Online (Sandbox Code Playgroud)
调用 getSystemService,返回 null:
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val sms = applicationContext.getSystemService(SmsManager::class.java)
}
Run Code Online (Sandbox Code Playgroud)
注意:我也尝试将其放入 onCreate() 中,其中调用 super.onCreate() ,同样的问题。
是否有其他方法可以获取上下文来访问 SmsManager 或服务进程内的任何其他系统服务?
从首次调用的主要活动中传递上下文是否明智,因为该服务将比打开的应用程序更持久?
我为我的最后一年项目创建了一个距离计算器.计算器应显示用户当前位置,然后在按下时在地图上显示标记.距离将从用户当前位置显示到标记.
我检索了用户位置并存储为我在代码中使用的变量但是我被抛出了java.lang.IllegalStateException: System services not available to Activities before onCreate()错误.我已尝试从onCreate()方法开始放置我的代码,但这也不起作用.任何帮助将不胜感激.我一直在努力让它工作但没有运气.当我尝试放置(LocationManager)getSystemService(Context.LOCATION_SERVICE);在onCreate()它需要一个许可,我用尽了一切.
这是我的代码
package com.example.matthewmcnabb.moyola;
import android.Manifest;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import …Run Code Online (Sandbox Code Playgroud) 我怀疑由ServiceManager注册的服务而不是由SystemServiceRegistry注册的服务。
/**
* Manages all of the system services that can be returned by {@link Context#getSystemService}.
* Used by {@link ContextImpl}.
*/
Run Code Online (Sandbox Code Playgroud)
这意味着可以从上下文中获取由System注册的服务。
关于ServiceManager,如何访问未由SystemServiceRegistry注册的应用程序中由ServiceManager添加的服务?
android ×4
google-maps ×1
installation ×1
java ×1
kotlin ×1
mongodb ×1
null ×1
privileges ×1
statusbar ×1