Skip to main content

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>


Source: Tips4all
Source: Tips4allSource: CCNA FINAL EXAM

Comments

  1. 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.

    ReplyDelete
  2. I think in some of the Android OS sometimes the OS kills the threads that are running while the Devices Boots which Android is not familiar with or doesn't recognize it. This is the reason why the TimerTask is working in some Devices and in some Devices just works for a 5-10 seconds and then the Application is ForceStopped automatically by the Android OS on Device Boot(Note - Its Force Stop from Manage Application and not Force close so I am not getting any error in the Logcat).

    So in that case the solution is to use the inbuilt Mechanism which Android OS recognizes and doesn't kill it and keeps it in a running mode. In this case I managed using AlarmManager to perform my task and it works.

    I might not be right but my final solution was to use AlarmManager to make my Application working in every Device.

    @Override
    public void onReceive(Context context, Intent arg1) {

    Log.v("Device Booted", "************ DEVICE BOOTED ******************");
    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);
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex