Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I want to pop up the alert dialog on location change through service. Here i have attached my manifest , Service and broadcastReciever codes.

In the console it is giving that .apk has been installed.

But i'm not getting any toast or alertdialog.

PLease correct me if i'm wrong.

Thanks.

Java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ser"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="3" />
    
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <receiver android:name="com.ser.Myreceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" /> 
        <category android:name="android.intent.category.HOME" />  
         
    </intent-filter>  
</receiver>

        
    <service  android:enabled="true" 
        android:name="com.ser.RunService">
    <intent-filter>  
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
        </service>    
        
        
    </application>

</manifest>


Java
package com.ser;

import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;



public class RunService extends Service implements LocationListener{

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

	@Override
    public void onCreate() {
        // TODO Auto-generated method stub

        System.out.println("**inside onCreate");
        super.onCreate();
        Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
        //Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:+5555")); 
        //startActivity(call);
    }
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("ALERT")
		   .setTitle("Location")
		   .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		       public void onClick(DialogInterface dialog, int id) {
		            
		       }
		   });
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}



Java
package com.ser;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
//import android.widget.Toast;
//import android.util.Log;

public class Myreceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("MYRECEIVER");
			//Toast.makeText(Myreceiver.this, "MyReciver", Toast.LENGTH_SHORT).show();
		     Intent serviceLauncher = new Intent(context, RunService.class);
		     context.startService(serviceLauncher);
		     //Log.v("TEST", "Service loaded at start");
		  
	}

}
Posted
Updated 22-May-12 19:56pm
v2

Hi,
You can open any UI in service, better way make new activity with Theme as dialog

Java
<activity android:name=".popup.PopupChatActivity" xmlns:android="#unknown">
		    android:theme="@android:style/Theme.Dialog"
		    android:configChanges="keyboardHidden|orientation"
			android:screenOrientation="portrait" 
			></activity>


You can pass intent to activity.


Thank you.
 
Share this answer
 
@tatvamobile thank you very much sir.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900