通过服务更改位置的Android-Alert对话框

时间:2012-05-25 19:08:14

标签: android service broadcastreceiver

我想通过服务弹出位置变更的警告对话框。在这里,我附上了我的清单,服务和广播执行代码。

在控制台中,它已经安装了.apk。

但我没有得到任何祝酒或警告。

如果我错了,请纠正我。

感谢。

<?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>`

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

}

 }

// Reciver

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");

}

 }

2 个答案:

答案 0 :(得分:0)

在此活动中创建一个活动并注册接收者并从此活动启动服务并在活动类中使用警报对话框

答案 1 :(得分:0)

我修改了你的代码,并且我的测试移动设备(索尼xperia)正常工作


清单文件

<?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="8" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
   <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">        
    <service android:enabled="true" android:name=".MyService">  
        <intent-filter>  
            <action android:name="com.ser.MyService">  
            </action>  
        </intent-filter>
    </service>  

    <receiver android:enabled="true" android:name=".MyReceiver">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED">  
        </action></intent-filter>  
    </receiver> 

    </application> 

MyReceiver.java

package com.ser;

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

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {     
        Toast.makeText(context, "MyReceiver Started..",Toast.LENGTH_SHORT).show();
        Log.v("Debug", "MyReceiver Started..");
        Intent myIntent=new Intent(context,MyService.class);        
        context.startService(myIntent);
    }
}

MyService.java

package com.ser;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

    private LocationManager locManager;
    private LocationListener locListener = new myLocationListener();

    private boolean gps_enabled = false;
    private boolean network_enabled = false;

    @Override
    public IBinder onBind(Intent intent) {return null;}

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        Toast.makeText(getBaseContext(), "Service Started..", Toast.LENGTH_SHORT).show();
        Log.v("Debug", "Service Started..");        

        locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
        try{
            gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        }
        catch(Exception ex){}
        try{
            network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);           
        }
        catch(Exception ex){}

        if (gps_enabled) {
            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
            Log.v("Debug", "gps_enabled..");
        }
        if (network_enabled) {
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
            Log.v("Debug", "network_enabled..");
        }   

        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }


    private class myLocationListener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {

            if(location!=null){
            Toast.makeText(getBaseContext(),"on location changed called..",Toast.LENGTH_SHORT).show();
            Log.v("Debug", "on location changed method called..");
            }
        }

        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}          

    }
}//service closed

仍然对接收器,服务和GPS课程有任何疑问让我知道!

甚至您可以查看Start Service from Broadcast ReceiverGet Current Location链接了解详情。希望它有所帮助!!