【问题标题】:Background service Thread is run on some phone but some phone not后台服务线程在某些手机上运行,​​但某些手机没有
【发布时间】:2017-05-30 14:49:15
【问题描述】:

我制作了一个应用程序,我编写了检测 wifi 网络并将数据发送到服务器的服务,为此我编写了警报管理器和广播接收器,然后通过警报管理器和广播接收器调用服务 onCreate 方法。我的代码如下:

BootCompletedIntentReceiver 类调用广播接收器用 30 秒。

public class BootCompletedIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        AlarmManager service = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent background = new Intent(context, AlarmReceiver.class);
            PendingIntent pending = PendingIntent.getBroadcast(context, 0, background,
                PendingIntent.FLAG_CANCEL_CURRENT);
            Calendar cal = Calendar.getInstance();
            // start 30 seconds after boot completed
            cal.add(Calendar.SECOND, 30);
            // fetch every 30 seconds
            // InexactRepeating allows Android to optimize the energy consumption
            service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(),2000, pending);


        }
    }

AlarmReceiver 类

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent background = new Intent(context, MyService.class);
        context.startService(background);
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);


            int icon = R.drawable.ic_launcher;
            CharSequence tickerText = "Service Started";
            long when = System.currentTimeMillis();

            Notification notification = new Notification(icon,tickerText, when);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            CharSequence contentTitle = "Service Start!";
            CharSequence contentText = "";
            Intent notificationIntent = new Intent(context,AlarmReceiver.class);
            PendingIntent contentIntent = PendingIntent
                    .getActivity(context, 0, notificationIntent, 0);

            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

            mNotificationManager.notify(1, notification);
            // Log.d("test", "Saving Data to File from Service.");
    }

}

从 AlarReceiver 服务获取调用,首先调用 onCreate 方法,然后调用 OnstartCommand.. 我编写线程来检测 wifi.. 此代码针对某些手机运行,但某些手机服务从未启动。为什么会这样??

我的清单文件代码如下:

<service android:name="com.edbeans.attendance.MyService"
                 android:enabled="true" 
                 android:process=":my_process" 
                 android:exported="true"
                 android:icon="@drawable/ic_launcher"></service>

         <receiver android:name="com.edbeans.attendance.AlarmReceiver">
             <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
         </receiver>
         <receiver android:name="com.edbeans.attendance.BootCompletedIntentReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

为什么会发生这种情况,即服务从某些电话获得呼叫并成功运行,但某些电话服务从未启动..

【问题讨论】:

  • 使用精确重复报警!
  • 哪部分手机没有在某些手机上运行,​​也请告诉我们您在哪部手机上得到了结果,在哪部手机上没有结果
  • @MuhammadBabar 我已经在 BootCompletedIntentReceiver 类中使用过。
  • @CoasMckey 在我公司的一些本地标签和我朋友的三星型号 GT-I8552 的手机上运行。
  • @kiko283 stackoverflow.com/questions/24750860/… 我试过你的代码,它的工作绝对正确。但我有一个疑问,我如何将警报设置为在 5 秒后连续检查。在您的代码中。

标签: android service broadcastreceiver


【解决方案1】:

在您收到的警报运行后,系统可能会立即返回低功耗状态。为了确保您的Service 运行,您需要使用唤醒锁在BroadcastReceiverService 之间进行协调。查看WakefulReceiver 作为帮助解决此问题的选项。

【讨论】:

猜你喜欢
  • 2018-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
  • 2018-11-09
相关资源
最近更新 更多