【问题标题】:AutoStart Application not working properly自动启动应用程序无法正常工作
【发布时间】:2012-01-27 07:09:45
【问题描述】:

我有一个简单的AutoStart 应用程序和TimerTask 实现,几乎可以在许多设备上正常工作。问题是它在Samsung Galaxy Y(2.3.6)DELL XCD35(2.2) 中不起作用。当设备启动时TimerTask 工作几秒钟然后关闭。我检查了Application->Manage Application,我看到应用程序已经处于Force Stop 状态。这意味着我的应用程序如何在几秒钟后停止。那么,这两个设备中weird behaviour 的原因是什么,如果有人有解决方案,请分享它。

下面是我的代码。

MyReceiver.java

public class MyReceiver extends BroadcastReceiver{

    private Timer mTimer = new Timer();
    @Override
    public void onReceive(Context context, Intent arg1) {
        Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show();
        Log.d("TAG","Device Booted");
        mTimer.scheduleAtFixedRate(new MyTimerTask(), 2000,2000);
    }

    private class MyTimerTask extends TimerTask
    {
        @Override
        public void run() {
            Log.d("TAG","TimerTask executed....");
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.autostart.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".MyReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

【问题讨论】:

  • 啊!!!任何反对投票的理由?
  • 一个有效的、格式良好的问题,不需要投反对票...

标签: android android-layout android-widget android-manifest


【解决方案1】:

我会建议你使用 AlarmManager 而不是 TimerTask,因为我遇到了你在许多设备中描述的同样问题。

    public void onReceive(Context context, Intent arg1) {
    Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show();
    Log.d("TAG","Device Booted");
AlarmManager AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent();
    intent.setAction("ALARM_MANAGER_ACTION");//can add any string action here
    PendingIntent pi = PendingIntent.getBroadcast(mContext
                                        .getApplicationContext(), 0, intent,0);
AM.set(AlarmManager.RTC,selectedTime, pi);
AM.setRepeating(AM.RTC_WAKEUP, System.currentTimeMillis()+2000, 2000, pi);
    }


  public class MyReceiver1 extends BroadcastReceiver{ 
  //event will come here
  private Timer mTimer = new Timer();
  @Override
  public void onReceive(Context context, Intent arg1) {
 // check if event is same as you broadcasted through alarmManager
    Toast.makeText(context, "Device Booted", Toast.LENGTH_LONG).show();
     Log.d("TAG","TimerTask executed....");

}

在你的应用中添加一个广播接收器,它应该监听("ALARM_MANAGER_ACTION") 动作。并将此操作添加到清单文件中。 我敢打赌它也可以在这两种设备上使用。

【讨论】:

    【解决方案2】:

    我认为在某些Android OS 中,有时操作系统会杀死正在运行的线程,而Android 不熟悉或无法识别它的Devices Boots。这就是为什么 TimerTask 在某些设备上工作而在某些设备上只工作 5-10 秒然后应用程序是 ForceStopped automatically 由 Android 操作系统在 Device Boot(注意 - 它是从管理应用程序强制停止而不是强制关闭,所以我在 Logcat 中没有收到任何错误)。

    所以在这种情况下,解决方案是使用inbuilt Mechanism Android OS 识别并且不会杀死它并使其保持在运行模式。在这种情况下,我设法使用AlarmManager 来执行我的任务并且它有效。

    我可能不对,但我的最终解决方案是使用AlarmManager 让我的应用程序在每个设备上都可以工作。

    @Override
        public void onReceive(Context context, Intent arg1) {
    
            Intent myIntent = new Intent(context, AlarmService.class);
            PendingIntent pendingIntent = PendingIntent.
                                             getService(context, 0, myIntent, 0);
            AlarmManager alarmManager = (AlarmManager) context
                                        .getSystemService(Context.ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                          System.currentTimeMillis() + 2000, 2000, pendingIntent);
        }
    

    更新:

    AlaramManager is critical system service that runs all the time.

    【讨论】:

      猜你喜欢
      • 2019-11-23
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      相关资源
      最近更新 更多