0

我是 Android 编程新手。我创建了一个警报管理器和广播管理器程序。广播管理器类被定义为主要活动中的内部类。但是内部类不起作用。我也在这里给出代码。请帮我。

public class Alarmactivity extends Activity {
    Button btn1;



    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn1=(Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                startAlert(null);

            }


        }); 

        }
     public void startAlert(View view) {        
         AlarmManager am=(AlarmManager)this.getSystemService(this.ALARM_SERVICE);
         Intent i = new Intent(this, Broadcas.class);
         PendingIntent pi = PendingIntent.getBroadcast(this, 0,i,0);
         am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 15, pi);



     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

     public class Broadcas extends BroadcastReceiver {

    @Override
     public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        System.out.println("**********************hello***********************************");


    }
}



}
4

1 回答 1

0

您是否在清单文件中使用正确的名称注册了接收器?在这种情况下,简单地给出 BroadcastReceiver 名称是行不通的,大多数情况下您需要使用该名称

报警活动$广播

编辑: 这是我的活动课:

public class MainTestActivity extends Activity {

    private Button send_broadcast = null;

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

        send_broadcast = (Button) findViewById(R.id.send_broadcast);
        send_broadcast.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startAlert(null);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_test, menu);
        return true;
    }

    public void startAlert(View view) {        
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(this, Broadcas.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 15, pi);
    }

    public static class Broadcas extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            System.out.println("**********************hello***********************************");
        }
    }

}

这是我的清单文件

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.coding.test.MainTestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="MainTestActivity$Broadcas"></receiver>
    </application>

</manifest>
于 2013-07-11T04:25:48.013 回答