Tag: 服务

7 android有什么问题?

我有服务接收udp datagramm套接字 private void startServer(final UdpServerListener listener) { mStarted = true; mWakeLock.acquire(10 * 60 * 1000L /*10 minutes*/); Looper myLooper = Looper.myLooper(); new HandlerThread(“TcpServerThread”) { @Override protected void onLooperPrepared() { new Handler(getLooper()).post(() -> { try { byte[] data = new byte[8 * 1024]; InetAddress address = BnjUtils.getLocalIpAddress(); DatagramPacket datagramPacket = new DatagramPacket(data, data.length); mServerSocket = […]

如何在应用程序关闭时启动服务

我想在应用程序关闭时启动服务。 java代码 public class AudioRecorder extends Service { public static final String SENDER_ID = “274211616343”; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, ” MyService Created “, Toast.LENGTH_LONG).show(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, ” MyService Started”, Toast.LENGTH_LONG).show(); return START_STICKY; } […]

升级到Android 8.1后,startForeground将失败

将手机升级到8.1 Developer Preview后,我的后台服务不能再正常启动。 在我长时间运行的服务中,我实现了一个startForeground方法来启动在创建时调用的正在进行的通知。 @TargetApi(Build.VERSION_CODES.O) private fun startForeground() { // Safe call, handled by compat lib. val notificationBuilder = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID) val notification = notificationBuilder.setOngoing(true) .setSmallIcon(R.drawable.ic_launcher_foreground) .build() startForeground(101, notification) } 错误信息: 11-28 11:47:53.349 24704-24704/$PACKAGE_NAMEE/AndroidRuntime: FATAL EXCEPTION: main Process: $PACKAGE_NAME, PID: 24704 android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My channel pri=0 […]

Android:stopService在另一个Activity中

如何在另一项活动中停止我的服务? 我在summaryActivity中启动服务 SocketServiceIntent = new Intent(this, SocketService.class); SocketServiceIntent.putExtra(“MessageParcelable”, mp); startService(SocketServiceIntent); 并从我的summaryActivity启动我的statusActivity Intent intent = new Intent(SummaryActivity.this, StatusActivity.class); intent.putExtra(“MessageParcelable”, mp); startActivity(intent); 我的问题是,我不知道如何可以给我的状态活动SocketServiceIntent。