广播接收器没有收到

时间:2014-07-23 20:42:26

标签: android broadcast

我有一个永远不会被触发的广播接收器。 Intentservice与主要活动位于一个单独的类中,并且启动意图服务广播就好了,或者看起来如此,但主要活动中的广播接收器从未接收过该活动。我在广播接收器的方法中有一些记录,它们永远不会被显示,这让我相信程序永远不会到达那一点。我不知道它是否与我如何设置有关,无论是在清单还是什么。我只是不确定为什么程序永远不会进入广播接收器类。

MainActivity:

public class MainActivity extends FragmentActivity implements OnClickListener {
    static final String ACTION_RESP = "com.example.assignment6.CALL";
    public static final String CALL_NUMBER = "CALL_NUMBER";
    private Button callButton;
    private Button cancelButton;
    private EditText numberText;
    private long number;
    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;

    public class CallReceive extends BroadcastReceiver {
        private int number;
        private static final String TAG = "SERVICE";
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            Log.i(TAG, "Just before intent create");
            Intent intent1 = new Intent(Intent.ACTION_CALL);
            intent1.setData(Uri.parse("tel:" + number));
            context.startActivity(intent1);
            Log.i(TAG, "After startActivity");
        }
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        callButton = (Button) findViewById(R.id.scheduleButton);
        cancelButton = (Button) findViewById(R.id.cancelButton);
        numberText = (EditText) findViewById(R.id.numberText);
        callButton.setOnClickListener(this);
        cancelButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        number = Long.parseLong(numberText.getText().toString());
        showTimePickerDialog(arg0);
        showDatePickerDialog(arg0);

        Intent intent = new Intent(this, CallIntentService.class);
        intent.putExtra("year", year);
        intent.putExtra("month", month);
        intent.putExtra("day", day);
        intent.putExtra("hour", hour);
        intent.putExtra("minute", minute);
        intent.putExtra("number", number);
        IntentFilter filter = new IntentFilter(MainActivity.ACTION_RESP);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        startService(intent);
    }

    public void showDatePickerDialog(View v) {
        DatePickerFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
        year = newFragment.getYear();
        month = newFragment.getMonth();
        day = newFragment.getDay();
    }

    public void showTimePickerDialog(View v) {
        TimePickerFragment newFragment = new TimePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
        hour = newFragment.getHour();
        minute = newFragment.getMinute();
    }

    public int getYear() {
        return year;
    }
}

IntentService:

public class CallIntentService extends IntentService {
    private int year;
    private int month;
    private int day;
    private int minute;
    private int hour;
    private int number;
    private static final String TAG = "SERVICE";
    public CallIntentService() {
        super("PhoneCall");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            while (true) {
                Thread.sleep(60000);
                year = intent.getIntExtra("year", year);
                month = intent.getIntExtra("month", month);
                day = intent.getIntExtra("day", day);
                minute = intent.getIntExtra("minute", minute);
                hour = intent.getIntExtra("hour", hour);
                number = intent.getIntExtra("number", number);
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, hour);
                cal.set(Calendar.MINUTE, minute);
                cal.set(Calendar.DAY_OF_MONTH, day);
                cal.set(Calendar.MONTH, month);
                cal.set(Calendar.YEAR, year);
                Calendar currentCal = Calendar.getInstance();
                Log.i(TAG, "just before if statement");
                Log.i(TAG, currentCal.YEAR + "");
                Log.i(TAG, cal.YEAR + "");
                // Only checking year
                if(currentCal.YEAR == cal.YEAR){
                    Intent broadcastIntent = new Intent("your.intent");
                    broadcastIntent.setAction(MainActivity.ACTION_RESP);
                    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
                    broadcastIntent.putExtra(MainActivity.CALL_NUMBER, number);
                    sendBroadcast(broadcastIntent);
                    Log.i(TAG, "In if statement");
                }
            }
        } catch (InterruptedException ie) {

        }
    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.assignment6"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <application

        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service android:name=".CallIntentService" />

        <receiver
            android:name="CallReceive"
            android:enabled="true" >
            <intent-filter>
                <action android:name="CALL_NUMBER" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>
</manifest>

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

static final String ACTION_RESP = "com.example.assignment6.CALL";

static final String ACTION_RESP = "com.example.CallIntentService";

答案 1 :(得分:0)

也许改变一下:

<receiver
        android:name="CallRecieve"
        android:enabled="true" >

进入(注意名称中的点):

<receiver
        android:name=".CallRecieve"
        android:enabled="true" >