0

I am creating an app on Android, which involves an Activity and a service. One of the components of the service is meant to send a signal to the MainActivity, which will execute some specific code. The problem is, trying to send the intent makes the app crash.

Here is the code that sends the intent. Note that it resides in the inside of a doInBackground function of an AsyncTask object, which resides inside the said service:

    @Override
    protected String doInBackground(Socket... client) {
        Intent received = new Intent();
        received.setAction("com.mycompany.Messenger.MESSAGE_RECEIVED");
        sendBroadcast(received);

    }

Note that if I comment out sendBroadcast(received), the app does not crash.

Here is the handler of the Intent inside MainActivity (which has the name ChatBubbleActivity in my app):

  public class MessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(MESSAGE_RECEIVED)) {
            String current_message = "Intent works!";
            TextView textView = (TextView) findViewById(R.id.myText);
            textView.setText(current_message);
        }
    }
}

And this is the declaration of the Receiver in manifest:

  <receiver android:name=".ChatBubbleActivity$MessageReceiver">
            <intent-filter>
                <action android:name="com.mycompany.Messenger.MESSAGE_RECEIVED"/>
            </intent-filter>

 </receiver>

, inside the <activity> chunk. What is it that I might be missing?

EDIT:New version of code based on answers, still crashing. EDIT logcat:

02-04 16:17:32.444      487-489/? D/dalvikvm﹕ GC_CONCURRENT freed 1662K, 22% free 6885K/8728K, paused 2ms+2ms, total 42ms
02-04 16:17:32.876    1275-1275/? D/AndroidRuntime﹕ >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
02-04 16:17:32.912    1275-1275/? D/AndroidRuntime﹕ CheckJNI is OFF
02-04 16:17:32.960    1275-1275/? D/dalvikvm﹕ Trying to load lib libjavacore.so 0x0
02-04 16:17:32.964    1275-1275/? D/dalvikvm﹕ Added shared lib libjavacore.so 0x0
02-04 16:17:32.968    1275-1275/? D/dalvikvm﹕ Trying to load lib libnativehelper.so 0x0
02-04 16:17:32.972    1275-1275/? D/dalvikvm﹕ Added shared lib libnativehelper.so 0x0
02-04 16:17:32.972    1275-1275/? D/dalvikvm﹕ No JNI_OnLoad found in libnativehelper.so 0x0, skipping init
02-04 16:17:33.020    1275-1275/? D/dalvikvm﹕ Note: class Landroid/app/ActivityManagerNative; has 179 unimplemented (abstract) methods
02-04 16:17:33.128    1275-1275/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
02-04 16:17:33.132    1275-1275/? E/android.os.Debug﹕ failed to load memtrack module: -2
02-04 16:17:33.264    1275-1275/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am
02-04 16:17:33.284      487-617/? I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.mycompany.messenger/com.mycompany.Messenger.ChatBubbleActivity} from pid 1275
02-04 16:17:33.692    1275-1275/? D/AndroidRuntime﹕ Shutting down VM
02-04 16:17:33.692    1275-1279/? D/dalvikvm﹕ GC_CONCURRENT freed 95K, 15% free 576K/676K, paused 1ms+0ms, total 1ms
02-04 16:17:33.884      487-538/? I/ActivityManager﹕ Start proc com.mycompany.messenger for activity com.mycompany.messenger/com.mycompany.Messenger.ChatBubbleActivity: pid=1286 uid=10059 gids={50059, 3003}
02-04 16:17:33.932    1286-1286/? D/dalvikvm﹕ Late-enabling CheckJNI
02-04 16:17:34.228    1286-1286/? D/dalvikvm﹕ GC_FOR_ALLOC freed 60K, 4% free 2873K/2992K, paused 6ms, total 7ms
02-04 16:17:34.236    1286-1286/? I/dalvikvm-heap﹕ Grow heap (frag case) to 3.338MB for 500412-byte allocation
02-04 16:17:34.248    1286-1295/? D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 3362K/3484K, paused 11ms, total 11ms
02-04 16:17:34.252    1286-1290/? D/dalvikvm﹕ GC_CONCURRENT freed 0K, 4% free 3362K/3484K, paused 1ms+0ms, total 5ms
02-04 16:17:34.696    1286-1286/? D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
02-04 16:17:34.708    1286-1286/? D/﹕ HostConnection::get() New Host Connection established 0xb7944cb0, tid 1286
02-04 16:17:34.744    1286-1286/? D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-04 16:17:34.744    1286-1286/? D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
02-04 16:17:34.816    1286-1286/? W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
02-04 16:17:34.820    1286-1286/? E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
02-04 16:17:34.824    1286-1286/? E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 8192
02-04 16:17:34.848    1286-1286/? E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-04 16:17:34.848    1286-1286/? E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 8192
02-04 16:17:34.852    1286-1286/? D/OpenGLRenderer﹕ Enabling debug mode 0
02-04 16:17:34.932    1286-1300/? W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0xa4d31b20)
02-04 16:17:34.932    1286-1300/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    Process: com.mycompany.messenger, PID: 1286
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
            at android.app.ContextImpl.startActivity(ContextImpl.java:1050)
            at android.app.ContextImpl.startActivity(ContextImpl.java:1037)
            at android.content.ContextWrapper.startActivity(ContextWrapper.java:311)
            at com.mycompany.Messenger.LocalService$DownloadWebpageTask2.doInBackground(LocalService.java:118)
            at com.mycompany.Messenger.LocalService$DownloadWebpageTask2.doInBackground(LocalService.java:113)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
02-04 16:17:34.936      487-617/? W/ActivityManager﹕ Force finishing activity com.mycompany.messenger/com.mycompany.Messenger.ChatBubbleActivity
02-04 16:17:35.180      487-840/? W/InputMethodManagerService﹕ Focus gain on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@529ef2ac (uid=10059 pid=1286)
02-04 16:17:35.288      487-617/? I/WindowManager﹕ Screenshot max retries 4 of Token{52998cec ActivityRecord{5294f214 u0 com.mycompany.messenger/com.mycompany.Messenger.ChatBubbleActivity t4 f}} appWin=Window{52948c58 u0 Starting com.mycompany.messenger} drawState=4
02-04 16:17:35.288      487-617/? W/WindowManager﹕ Screenshot failure taking screenshot for (800x1280) to layer 21015
02-04 16:17:38.512      487-502/? I/Choreographer﹕ Skipped 188 frames!  The application may be doing too much work on its main thread.
02-04 16:17:38.516      711-711/? I/Choreographer﹕ Skipped 185 frames!  The application may be doing too much work on its main thread.
02-04 16:17:39.708      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:17:40.536      711-711/? W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
02-04 16:17:41.260      487-502/? I/Choreographer﹕ Skipped 35 frames!  The application may be doing too much work on its main thread.
02-04 16:17:49.560    1286-1300/? I/Process﹕ Sending signal. PID: 1286 SIG: 9
02-04 16:17:49.580      487-616/? W/InputMethodManagerService﹕ Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@52963b68 attribute=null, token = android.os.BinderProxy@5290713c
02-04 16:17:49.644      487-840/? I/ActivityManager﹕ Process com.mycompany.messenger (pid 1286) has died.
02-04 16:17:49.716      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:17:59.720      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:18:09.728      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:18:19.736      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:18:29.744      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:18:39.752      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:18:49.756      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:18:59.764      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:19:09.772      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:19:19.780      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:19:29.788      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:19:39.792      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
02-04 16:19:49.800      117-117/? D/local_opengl﹕ Select interrupted or nothing to read
Noob Doob
  • 1,757
  • 3
  • 19
  • 27

2 Answers2

0

Do you want to start an activity or send a broadcast? I don't understand your code.

You are starting an activity from a background thread, that's not allowed.

In your AsyncTask, the method protected String doInBackground(Socket... client) is the only one that doesn't run in the UI thread.

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • The `startActivity` method was recommended in some Android intent tutorials. I just want to send an Intent in an already running Activity, so I guess my approach is wrong? What should I use then? – Noob Doob Feb 04 '15 at 16:23
0

When ever you want to start your Broadcast Receiver you don't call it as an acitivty

Your Method

Intent received = new Intent(ChatBubbleActivity.MESSAGE_RECEIVED);
received.putExtra("current", "Intent is working!!");
startActivity(received);

What it should be

Intent intent = new Intent();
intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
sendBroadcast(intent);

Have a look at these tutorials

MDMalik
  • 3,951
  • 2
  • 25
  • 39
  • now remove it from doBackground and place it in `onPostExecute` – MDMalik Feb 04 '15 at 16:31
  • Again crashes :'( shall I post the entire code in Code Review? – Noob Doob Feb 04 '15 at 16:36
  • 6
    Note that Code Review is for working code only. Code that does not work will be closed as off-topic – rolfl Feb 04 '15 at 16:38
  • 4
    If it is crashing, please keep it off Code Review! It will get closed if it is posted there and does not work. –  Feb 04 '15 at 16:38
  • 4
    @NoobDoob no please not. CodeReview is for **working** code only. This means no crashes ;) for more information, see the [help center over there](http://codereview.stackexchange.com/help/on-topic) – Vogel612 Feb 04 '15 at 16:38
  • What bugs me is that I have made it as simple as possible just to execute one single Intent and see it works, but it does not. Would it be helpful to post the entire files here instead? If yes, inform me what you might need, taking into account it's an Android project. – Noob Doob Feb 04 '15 at 16:39
  • 1
    Sorry I didn't knew that.. thanks for the advice. My apologize – MDMalik Feb 04 '15 at 16:40
  • Moreover (maybe I should also put this in the question) executing the intent from inside the `doInBackground` does matter to me, because eventually I want a thread constantly running that will need to inform MainActivity for events (here receiving data from a server). Maybe using AsyncTask is not the recommended way for that, but I don't think it will hurt for a first test. – Noob Doob Feb 04 '15 at 16:43