Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

android - AutoStart Application not working properly

I am having a Simple AutoStart Application with TimerTask implementation, that works fine in almost many devices. The problem is that it is not working in Samsung Galaxy Y(2.3.6) and DELL XCD35(2.2). When the device boots TimerTask works for some seconds and then shuts down. I check in the Application->Manage Application, I saw that the Applcation was already in Force Stop State. That means some how my Application gets stopped after some seconds. So, what is the reason for this weird behaviour in these two devices, if anyone has the solution do share it.

Below is my code.

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I will suggest you to use AlarmManager instead of TimerTask, as I faced the same problem you described in many devices.

    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....");

}

add a Broadcast receiver in your app, which should listen ("ALARM_MANAGER_ACTION") action. and add this action into manifest file. I bet it will work in these two devices as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...