0

我想通过服务弹出有关位置更改的警报对话框。在这里,我附上了我的清单、服务和广播接收器代码。

在控制台中,它表明 .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

}

 }

//接收者

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

}

 }
4

2 回答 2

0

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

于 2012-05-26T09:25:39.083 回答
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);
    }
}

我的服务.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 类仍有任何疑问,请告诉我!!

甚至您可以检查从广播接收器启动服务获取当前位置链接以获取详细信息。希望能帮助到你!!

于 2012-05-28T07:39:36.280 回答