问题描述:

I am trying to program a simple app, and during this I got stuck on this alarm thing.

I am very new to android and programing at all, so my mistake might just be a dumb one, but no matter what i try the 'onReceive' method won't start.

If this has any influence- I am programing for android 2.3.3 (API 10).

this is my activity:

package itamar.fruchter.sendi;

import java.util.Calendar;

import android.os.Bundle;

import android.app.Activity;

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.DialogInterface.OnClickListener;

import android.content.Intent;

import android.telephony.SmsManager;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class Simple extends Activity implements android.view.View.OnClickListener

{

public EditText etTo;

public EditText etTxt;

public Button btnSend;

private PendingIntent pendingIntent;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.semd);

Log.i("iiiiiiiiii", "at least");

etTo = (EditText) findViewById(R.id.pn);

etTxt = (EditText) findViewById(R.id.text);

btnSend = (Button) findViewById(R.id.send);

}

@Override

public boolean onCreateOptionsMenu(Menu menu)

{

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.simple, menu);

return true;

}

@Override

public void onClick(View v)

{

if(v.equals(btnSend))

{

String pn = etTo.getText().toString();

String txt = etTxt.getText().toString();

Intent myIntent = new Intent(this, Reciver.class);

Bundle bundle = new Bundle();

bundle.putCharSequence("Number", pn);

bundle.putCharSequence("Message", txt);

myIntent.putExtras(bundle);

pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

calendar.add(Calendar.SECOND,30);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Toast.makeText(getApplicationContext(), "Message: "+txt+" for Number: "+pn, Toast.LENGTH_SHORT).show();

Log.i("iiiiiiiiii", "sent");

}

}

}

(with the layout there shouldn't be any problem, beacause when I run the app I see the toast)

this is my BroadcastReceiver:

package itamar.fruchter.sendi;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.app.Activity;

import android.os.Bundle;

import android.telephony.SmsManager;

import android.util.Log;

import android.widget.Toast;

public class Reciver extends BroadcastReceiver

{

@Override

public void onReceive(Context context, Intent intent)

{

Log.i("iiiiiiiiii", "recived");

Toast.makeText(context, "recived", Toast.LENGTH_LONG).show();

}

}

Now here, I don't get any of those messages.

Just to be on the safe side, this is my Manifest:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="itamar.fruchter.sendi"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="10"

android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.SEND_SMS"/>

<application

android:allowBackup="true"

android:enabled="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="itamar.fruchter.sendi.MainActivity"

android:label="@string/app_name" >

</activity>

<activity

android:name="itamar.fruchter.sendi.SendActivity"

android:label="@string/title_activity_send" >

</activity>

<activity

android:name="itamar.fruchter.sendi.Simple"

android:label="@string/title_activity_simple" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service android:name="mService" android:enabled="true" />

<reciver android:name="Reciver">

</reciver>

</application>

</manifest>

网友答案:

First, I am not sure if it is a typo, but you should use .Receiver instead of Receiver in your Manifest and add an intent-filter

<receiver android:name=".Reciver">
    <intent-filter>
        <action android:name="com.example.broadcast.message" />
    </intent-filter>
</receiver>

And then instantiate the intent with this:

static final STRING BROADCAST_ACTION_STRING="com.example.broadcast.message";

Intent myIntent = new Intent(BROADCAST_ACTION_STRING);

Other code is fine. The receiver should be able to work with the above change.

You can refer to this post as reference.

相关阅读:
Top