android:警报已经启动了onReceive

时间:2014-04-25 07:29:16

标签: java android eclipse broadcastreceiver alarmmanager

我正在尝试编写一个简单的应用程序,在此期间我遇到了这个警报的事情。 我对android和编程都很陌生,所以我的错误可能只是一个愚蠢的,但无论我尝试什么'onReceive'方法都无法启动。 如果这有任何影响 - 我正在为android 2.3.3(API 10)编程。

这是我的活动:

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

(布局不应该有任何问题,因为当我运行应用程序时,我看到吐司)

这是我的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();
    }
}        

现在,我没有收到任何这些消息。

为了安全起见,这是我的清单:

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

1 个答案:

答案 0 :(得分:0)

首先,我不确定它是否是拼写错误,但你应该在你的清单中使用.Receiver而不是Receiver并添加一个意图过滤器

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

然后用这个实例化意图:

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

Intent myIntent = new Intent(BROADCAST_ACTION_STRING);

其他代码没问题。接收方应该能够处理上述变化。

您可以参考this post as reference