-1

ユーザーが半径に入ったときにテキストを送信する GPS 近接センサーのコードを作成しようとしています。これが私がこれまでに持っているものですが、何も起こりません。マニフェスト内のレシーバーについては何も宣言していません。そうすると、必須ではないと言われます。

package com.example.drivetext;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;

public class targetdistance extends Activity {

double finalc1;
double finalc2;
int finalsd;
String finalmessage;
String finalnumber;
private LocationManager locationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.target);

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Intent intent = getIntent();
    String contactNo;
    contactNo = intent.getStringExtra("PhoneNumber");
    finalnumber = contactNo;
    String message;
    message = intent.getStringExtra("TextMessage");
    finalmessage = message;
    double coord1 = 0;
    coord1 = intent.getDoubleExtra("Coordinate1", coord1);
    finalc1 = coord1;
    double coord2 = 0;
    coord2 = intent.getDoubleExtra("Coordinate2", coord2);
    finalc2 = coord2;
    int seldis = 0;
    seldis = intent.getIntExtra("SelectedDistance", seldis);
    finalsd = seldis;

    double lat = finalc1;
    double long1 = finalc2;    //Defining Latitude & Longitude
    float radius=finalsd;                         //Defining Radius
    long expiration = -1;

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Intent intent1 = new Intent(); //proximityIntentAction

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
    locationManager.addProximityAlert(lat, long1, radius, expiration, pendingIntent);

}

public void onReceive(Context context, Intent intent)
{
    Log.v("SomeTag","Proximity Alert Intent Received");
    String direction = LocationManager.KEY_PROXIMITY_ENTERING;

    SmsManager smsManager = SmsManager.getDefault();
    String sendTo = finalnumber;
    String myMessage = finalmessage;
    smsManager.sendTextMessage(sendTo, null, myMessage, null, null);
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_NO_CREATE);
    locationManager.removeProximityAlert(pendingIntent);
}

}

4

1 に答える 1

0

次の方法で受信者を AndroidManifest に登録する必要があります。

 <receiver android:name="MyScheduleReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
    </receiver>

あなたのブロードキャストレシーバークラスはどこにありますか? BroadcastReceiver を拡張してレシーバー クラスを作成する必要があります。

MyReceiver extends BroadcastReceiver

これは、Androidでレシーバーを使用する方法に関するチュートリアルです

于 2014-05-13T07:16:49.803 に答える