Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android广播接收器不工作_Android - Fatal编程技术网

android广播接收器不工作

android广播接收器不工作,android,Android,这是我根据一本Press Android Recipes book编写的应该工作的代码: package com.tugce.AlarmActivity; public class AlarmActivity extends Activity implements OnClickListener { private PendingIntent mAlarmIntent; /** Called when the activity is first created. */

这是我根据一本Press Android Recipes book编写的应该工作的代码:

package com.tugce.AlarmActivity;
public class AlarmActivity extends Activity implements OnClickListener {

    private PendingIntent mAlarmIntent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);

        Intent launchIntent = new Intent(this,AlarmReceiver.class);
        mAlarmIntent = PendingIntent.getBroadcast(this, 0, launchIntent, 0);
    }

    @Override
    public void onClick(View v) {
        AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        long interval = 2000;

        switch (v.getId()) {
        case R.id.start:
            Toast.makeText(this, "Scheduled", Toast.LENGTH_SHORT).show();
            manager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()+interval, interval, mAlarmIntent);
            break;

        case R.id.stop:
            Toast.makeText(this, "Canceled", Toast.LENGTH_LONG).show();
            manager.cancel(mAlarmIntent);
            break;

            default:
                break;
        }

    }

    public class AlarmReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            Calendar now = Calendar.getInstance();
            DateFormat formatter = SimpleDateFormat.getDateTimeInstance();
            Toast.makeText(context, formatter.format(now.getTime()),
                    Toast.LENGTH_SHORT).show();

        }

    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tugce.AlarmActivity"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <receiver android:name=".AlarmReceiver"></receiver>
        <activity android:name=".AlarmActivity"
                  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>
package com.tugce.AlarmActivity;
公共类AlarmActivity扩展活动实现OnClickListener{
私人悬垂帐篷;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findviewbyd(R.id.start).setOnClickListener(this);
findViewById(R.id.stop).setOnClickListener(this);
Intent launchIntent=新Intent(这是AlarmReceiver.class);
malarminent=PendingIntent.getBroadcast(this,0,launchIntent,0);
}
@凌驾
公共void onClick(视图v){
AlarmManager=(AlarmManager)getSystemService(Context.ALARM\u服务);
长间隔=2000;
开关(v.getId()){
案例R.id.start:
Toast.makeText(这是“预定的”,Toast.LENGTH_SHORT).show();
manager.setRepeating(AlarmManager.Appeased_REALTIME,SystemClock.elapsedRealtime()+interval,interval,MalarMinent);
打破
案例R.id.stop:
Toast.makeText(此“已取消”,Toast.LENGTH_LONG).show();
经理:取消(mAlarmIntent);
打破
违约:
打破
}
}
公共类AlarmReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
Calendar now=Calendar.getInstance();
DateFormat格式化程序=SimpleDataFormat.getDateTimeInstance();
Toast.makeText(上下文,格式化程序.format(现在是.getTime()),
吐司。长度(短)。show();
}
}
}

我在屏幕上看不到任何时间戳。

你的接受者是内部职业吗?不应该这样。将其放在自己的Java文件中。Android无法以其他方式实例化它。

我自己没有尝试过,但您是否需要设置此权限: