English 中文(简体)
A. 关于通过服务改变地点的Adroid-Alert对话
原标题:Android-Alert Dialog on location change through service

我想通过服务打开关于地点变化的警示对话框。 这里我附上了个人名单、服务和广播代码。

它在控制台里给出了.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");

}

 }
问题回答

创建活动并在此活动中注册接收器并创建活动并注册接收器,启动此活动的服务,并在活动类别中使用提醒对话框

我用我的测试移动设备(Sony xperia) 修改了你的代码 及其操作正常


Manifest file

<?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课程有任何疑问,

或甚至可以查看 < a href=> "http://rdcworld-android.blogspot.in/2012/03/start-service-from-broadcast-rececast-receiver.html" rel=“nofollow” > start Service from 广播接收器 和 < a href="http://rdcworld-android.blogspot.in/2012/01/get- murd-place-coballs-calls-city.html" rel="nofollow" > Get the proad place 链接的详细信息。 希望它有用!





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签