Creating a very basic BroadcastReceiver

Put your problem here if it does not fit any of the other categories.

Creating a very basic BroadcastReceiver

Postby oneliner » Sun Apr 10, 2011 5:26 pm

Hi, I'm pretty new at Android but I like it a lot so I started developing with the Android developers cookbook.
I tried to make a simple BroadcastReceiver exercise but I doesn't want to work and I don't get it.
The purpose Is to push the call button so the BroadcastReceiver calls a service to show a Toast.
Can someone help me out?

I start with my Acitivity class:

Code: Select all
package com.cookbook.simple_receiver;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class SimpleActivity extends Activity {
   SimpleBroadcastReceiver intentReceiver = new SimpleBroadcastReceiver();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CALL_BUTTON);
        //intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
        registerReceiver(intentReceiver,intentFilter);
    }
   
    @Override
    protected void onDestroy(){
       unregisterReceiver(intentReceiver);
       super.onDestroy();
    }
}


Than I created a BroadcastReceiver class:

Code: Select all
package com.cookbook.simple_receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.TextView;
import android.widget.Toast;

public class SimpleBroadcastReceiver extends BroadcastReceiver{
   @Override
   public void onReceive(Context rcvContext,Intent rcvIntent){
      String action = rcvIntent.getAction();
      if(action.equals(Intent.ACTION_CALL_BUTTON)){
         rcvContext.startService(new Intent(rcvContext,SimpleService2.class));
      }
   }
}


This class calls a service which simply shows a toastmessage, but this message is never displayed.

Code: Select all
package com.cookbook.simple_receiver;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class SimpleService2 extends Service{

   @Override
   public IBinder onBind(Intent intent) {
      // TODO Auto-generated method stub
      return null;
   }

   @Override
   public void onCreate(){
      super.onCreate();
      Toast.makeText(this,"Service created...",Toast.LENGTH_LONG).show();
   }
   
   @Override
   public void onDestroy(){
      super.onDestroy();
      Toast.makeText(this,"Service destroyed ...",Toast.LENGTH_LONG).show();
   }
}
oneliner
Freshman
Freshman
 
Posts: 2
Joined: Fri Feb 18, 2011 2:52 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 25 guests