On the line that says:

showScreen( getApplicationContext() );

Try changing it to:

showScreen( context );

That might not be it, but it's best to use the context that is being passed 
to you in the onReceive. If that doesn't work, please post the error so we 
can help you more.

Steven
Studio LFP
http://www.studio-lfp.com


On Thursday, October 13, 2011 12:14:35 PM UTC-5, Diego Fernandez wrote:
>
> Hello to all, I have read something about how to launch a 
> Activity from BroadcastReceiver, hence now I am reached the point 
> where I really cant not find the answer to... why is closing my 
> application?. 
>
> I will explain the idea of ​​the application: 
>
> It consists of a main screen "main" that has only one 
> ToggleButton, when it changes state to "On" begins my 
> service, when go to "Off" does the same and stops it. 
> This all works fine, I can close the application and 
> service continue running smoothly. I can stop this service when I 
> want, reopening the application and changing the button to "OFF". 
> Now the service "MyService" is waiting any 
> change of your phone, for each change It send a message like a Toast, 
> it helps me to know and 
> check if my service is active, it works 
> perfectly except when you add the code lines to run an Activity that 
> contains 
> simply an image that is where the application is closed, if I leave 
> only the Toast, it is going great, tells me that 
> the phone are ringing and informs the incoming phone number. 
>
> I create a class that receives the 
> context and try to launch the Activity in question "NewScreen" 
>
> Here I leave the application code, Any idea? 
> It took me several days and I dont reach any breakthrough. 
>
> Thanks for advance 
>
> ----------------------------------------------------------------------------------------------------------------------
>  
>
>
> <?xml version="1.0" encoding="utf-8"?> 
> <manifest xmlns:android="http://schemas.android.com/apk/res/android"; 
> package="xx.com.xxxxx.myservice" 
> android:versionCode="1" 
> android:versionName="1.0"> 
> <uses-sdk android:minSdkVersion="4" /> 
> <uses-permission android:name="android.permission.VIBRATE"></uses- 
> permission> 
> <uses-permission 
> android:name="android.permission.READ_PHONE_STATE"></uses-permission> 
>
> <application 
> android:icon="@drawable/icon" 
> android:label="@string/app_name"> 
>
> <activity 
> android:label="@string/app_name" 
> android:screenOrientation="portrait" android:name="Main"> 
> <intent-filter> 
> <action android:name="android.intent.action.MAIN" /> 
> <category 
> android:name="android.intent.category.LAUNCHER" /> 
> </intent-filter> 
> </activity> 
>
> <activity 
> android:screenOrientation="portrait" 
> android:name="NewScreen"> 
> </activity> 
>
> <service 
> android:label="My Service" 
> android:enabled="true" 
> android:name="MyService"> 
> </service> 
> </application> 
> </manifest> 
>
> ----------------------------------------------------------------------------------------------------------------------
>  
>
>
> public class Main extends Activity{ 
>
> ToggleButton buttonStatus; 
> ActivityManager ActivityMan; 
> List<RunningServiceInfo> RunningServices; 
> boolean isMyServiceRunning = false; 
>
> @Override 
> public void onCreate(Bundle savedInstanceState) { 
> super.onCreate(savedInstanceState); 
> setContentView(R.layout.main); 
>
> ActivityMan = (ActivityManager) this.getSystemService 
> (ACTIVITY_SERVICE); 
> RunningServices = 
> ActivityMan.getRunningServices(Integer.MAX_VALUE); 
>
> buttonStatus = (ToggleButton) findViewById(R.id.buttonStatus); 
> buttonStatus.setOnCheckedChangeListener(listener); 
>
> checkServiceRuning(); 
> showStatusService(); 
> } 
>
> OnCheckedChangeListener listener = new OnCheckedChangeListener() { 
> public void onCheckedChanged(CompoundButton buttonView, 
> boolean isChecked) { 
> showStatusService(); 
> MyService(isChecked); 
> } 
> }; 
>
> public void checkServiceRuning(){ 
> for (RunningServiceInfo runningServiceInfo : RunningServices) 
> { 
> if 
> (runningServiceInfo.service.getClassName().equals(MyService.class.getName())) 
>
> { 
> isMyServiceRunning = true; 
> buttonStatus.setChecked(true); 
> } 
> } 
> } 
>
> public void MyService(boolean status){ 
> if (status) 
> startService(new Intent(getApplicationContext(), 
> MyService.class)); 
> else 
> stopService(new Intent(getApplicationContext(), 
> MyService.class)); 
> } 
>
> public void showStatusService(){ 
> if(buttonStatus.isChecked()) { 
> Toast.makeText(Main.this, "Service ON", 
> Toast.LENGTH_SHORT).show(); 
> } else { 
> Toast.makeText(Main.this, "Service OFF", 
> Toast.LENGTH_SHORT).show(); 
> } 
> } 
> } 
>
> ----------------------------------------------------------------------------------------------------------------------
>  
>
>
> public class MyService extends Service { 
>
> BroadcastReceiver phoneStateChangeReceiver; 
> Bundle extras; 
> String phoneNumber; 
>
> @Override 
> public void onCreate() { 
>
> phoneStateChangeReceiver = new PhoneStateChangeReceiver(); 
> registerReceiver(phoneStateChangeReceiver, new 
> IntentFilter(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)); 
>
> } 
>
> public void onDestroy() { 
> unregisterReceiver(phoneStateChangeReceiver); 
> } 
>
> public class PhoneStateChangeReceiver extends BroadcastReceiver { 
> @Override 
> public void onReceive(Context context, Intent intent) { 
> String newState = intent.getStringExtra("state"); 
> Toast.makeText(getApplicationContext(), "Phone state: " + newState, 
> Toast.LENGTH_SHORT).show(); 
>
> if 
> ( android.telephony.TelephonyManager.EXTRA_STATE_RINGING.equals(newState) ) 
>
> { 
> extras = intent.getExtras(); 
> phoneNumber = 
> extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
> Toast.makeText(getApplicationContext(), "Phone number: " + 
> phoneNumber, Toast.LENGTH_SHORT).show(); 
> showScreen(getApplicationContext()); 
> } 
> } 
> } 
>
> public void showScreen(Context context){ 
> Intent NewScreen = new Intent(context, MyScreen.class); 
> NewScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
> NewScreen.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
> context.startActivity(NewScreen); 
> } 
>
> @Override 
> public IBinder onBind(Intent arg0) { 
> // TODO Auto-generated method stub 
> return null; 
> } 
> }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to