2

1.How to launch/open application when the device is just open?

2.How to launch/open application when the device is just unlocked/exit from lock state?

(I mean, how to recognize these events?)

10q,

David

This is my new code:

In Manifest:

<receiver
  android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
  android:name="BootReciver" >
  <intent-filter >
      <action android:name="android.intent.action.BOOT_COMPLETED" />
      <action android:name="android.intent.action.SCREEN_ON" />
  </intent-filter>
</receiver>

In BootReciver.java I added to my project:

package development.Grandpa;
import android.content.BroadcastReceiver;

    public class BootReciver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Intent intent1 = new Intent(context, Grandpa.class);
        context.startActivity(intent1);      
    }
}

Where Grandpa class is my Main Activity.

In addition, I'm running the code directlly on my device and that is what I'm getting in the LogCat:

??-?? ??:??:??.???: INFO/(): Unable to open log device '/dev/log/main': No such file or directory

I also tried to add one reciver for boot and another one for ScreenOn (with two classes) like this:

<receiver
android:name="ScreenOnReciver" >
    <intent-filter >
    <action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>

<receiver
android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:name="BootReciver" >
<intent-filter >
    <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
 </receiver>

But it didn't help also.

Edit:

This it what I'm getting in logcat after:

1.Run the application

2.Exit from the application

3.Lock Screen

4.Unlock Screen

11-13 23:59:26.140: ERROR/AndroidRuntime(3640): Set to default setting_6 : region=-   Duser.region=IL propRegn=IL
11-13 23:59:26.710: ERROR/AndroidRuntime(3649): Set to default setting_6 : region=-Duser.region=IL propRegn=IL
11-13 23:59:33.246: ERROR/power(244): *** set_screen_state 0
11-13 23:59:33.246: ERROR/SensorManager(244): unregisterListener:: all sensors,    listener = com.android.server.PowerManagerService$13@40517118
11-13 23:59:33.593: ERROR/LockPatternKeyguardView(244): Phone is boot completed. so can   send broadcast message for music player
11-13 23:59:35.286: ERROR/power(244): *** set_screen_state 1
11-13 23:59:35.286: ERROR/SensorManager(244): registerListener :: handle = 3  name=     GP2A Light Sensor delay= 200000 Listener=   com.android.server.PowerManagerService$13@40517118
11-13 23:59:35.286: ERROR/SensorManager(244): reg :: handle = 3
11-13 23:59:35.336: ERROR/LockPatternKeyguardView(244): Phone is boot completed. so can  send broadcast message for music player
11-13 23:59:36.360: ERROR/SlidingTab(244): onTouchEvent() : thresholdReached !
11-13 23:59:36.380: ERROR/KeyguardViewMediator(244): Phone is boot completed. so can   broadcast
11-13 23:59:36.400: ERROR/MTPRx(3319): In MtpReceiverandroid.intent.action.USER_PRESENT
11-13 23:59:36.410: ERROR/MTPRX(3319): Battery charging. plugType = 2
11-13 23:59:36.410: ERROR/MTPRx(3319): USB charging
11-13 23:59:36.416: ERROR/MTPRx(3319): usb is connected, set value in Settings.System,   result = true
11-13 23:59:36.416: ERROR/MTPRx(3319): usb mode = 0
11-13 23:59:36.416: ERROR/MTPRx(3319): usb debugging is enabled

Edit2:

I have only these "Exception" in the logcat:

11-14 00:22:15.313: WARN/WindowManager(244): android.view.InflateException: Binary XML  file line #7: Error inflating class <unknown>
11-14 00:22:15.313: WARN/WindowManager(244): Caused by: java.lang.reflect.InvocationTargetException
11-14 00:22:15.313: WARN/WindowManager(244): Caused by:   android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or   path): TypedValue{t=0x2/d=0x1010059 a=-1}
6
  • I don't know why you are getting that log error, but I believe it is unrelated to this question. Either search for a solution or post a new question.
    – skynet
    Nov 13, 2011 at 14:12
  • I fix this problem according to: stackoverflow.com/questions/4867971/dev-log-main-not-found
    – David
    Nov 13, 2011 at 22:05
  • That log is not the stack trace. Look for lines that say "Exception at ..."
    – skynet
    Nov 13, 2011 at 22:07
  • Hi Craigy, Is it possible to ask you for a working example code via private mail?
    – David
    Nov 13, 2011 at 22:37
  • No, because I do not have a working example and then others would not be able to benefit from the process of working through your problems on SO. The error you currently face is spelled out in the logcat exception you posted. You are using a resource that is not a drawable in line 7 of one of your XML files.
    – skynet
    Nov 13, 2011 at 22:41

1 Answer 1

7

What you want to do is set up a BroadcastReceiver that listens for those actions. More information:

for your situation you would have a receiver

public class YourReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intent = new Intent(context, YourActivity.class);
        context.startActivity(intent);
    }
}

and register it in your manifest:

    <receiver
        android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:name="YourReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
12
  • 1
    Hi Craigy, 10x for the answer and the links. One more queation, where do I have to put the code above? In a seperate project? or in the same project?
    – David
    Nov 9, 2011 at 23:48
  • 1
    It will be in the same project. You will create a new Java class for the receiver, and add the <receiver> tag into your AndroidManifest.xml file inside the <application> tag.
    – skynet
    Nov 9, 2011 at 23:55
  • e.g: I have Application Project called 'A'. and in this project i want to add a code that everytime the device turn on or unlock the application ('A') will open. Can it be done using the above concepts?
    – David
    Nov 10, 2011 at 0:02
  • Hi again, the "startActivity(intent)" is not recognize, how to fix it?
    – David
    Nov 10, 2011 at 6:55
  • Good call, it should be context.startActivity(intent)
    – skynet
    Nov 10, 2011 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.