2014-01-29 82 views
3

我正在嘗試與broadcasReceiver一起使用報警管理器。我嘗試使用Tutorial: System Services and BroadcastReceiver中的示例gived。但是當我在時間結束後運行示例時,我沒有想要展示的敬酒。BroadcastReceiver和報警管理器Android

我的主要活動:

public class AlarmActivity extends Activity { 


    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_alarm); 
    } 

    public void startAlert(View view) { 
     EditText text = (EditText) findViewById(R.id.time); 
     int i = Integer.parseInt(text.getText().toString()); 
     Intent intent = new Intent(this, MyBroadcastReceiver.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
       + (i * 1000), pendingIntent); 
     Toast.makeText(this, "Alarm set in " + i + " seconds", 
       Toast.LENGTH_LONG).show(); 
    } 


} 

我的廣播接收器:

public class MyBroadcastReceiver extends BroadcastReceiver{ 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "Don't panik but your time is up!!!!.", 
       Toast.LENGTH_LONG).show(); 
     // Vibrate the mobile phone 
     Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 
     vibrator.vibrate(2000); 
    } 

} 

我的主要Laout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 

    <EditText 
      android:id="@+id/time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:hint="Number of seconds" 
      android:inputType="numberDecimal" > 
    </EditText> 

    <Button 
      android:id="@+id/ok" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="startAlert" 
      android:text="Start Counter" > 
    </Button> 

</LinearLayout> 

和manifest資源配置文件:

代碼是由下面的代碼給出
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.broadcastreceiver" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 
    <uses-permission android:name="android.permission.VIBRATE" > 
    </uses-permission> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.broadcastreceiver.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> 
     <receiver android:name=".MyBroadcastReceiver" > 
     </receiver> 
    </application> 

</manifest> 
+0

你能否更新鏈接到真正的教程,而不是主頁?很難找到您提到的沒有這個鏈接的正確例子。 – WarrenFaith

+0

@WarrenFaith完成 – zied

+0

@ user3156767,你有沒有得到這個工作?我嘗試過所有的東西,但仍然不起作用。 – Azurespot

回答

3

接收器必須從廣播接收器

延長
public class MyBroadcastReceiver extends BroadcastReceiver { 
    // ... 
} 

也可確保在清單文件接收器名稱是正確的。

+0

我的軟件包名稱在清單中是錯誤的, – zied

1

您MyBroadcastReceiver類是錯把這段代碼

public class MyBroadcastReceiver extends BroadcastReceiver 
{ 
@Override 
public void onReceive(Context context, Intent intent) { 
    Toast.makeText(context, "Don't panik but your time is up!!!!.", 
       Toast.LENGTH_LONG).show(); 
     // Vibrate the mobile phone 
     /*Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 
     vibrator.vibrate(2000);*/  
} 
} 

只需要添加「擴展廣播接收器」 並獲得的onReceive()方法重寫。

0

你只是忘了重寫OnReceive()方法。 public void onReceive(Context context, Intent intent)是廣播接收器的預定義的方法,wheneven你正在使用它,你必須按如下@Override它,

public class MyBroadcastReceiver extends BroadcastReceiver 
{ 
    @Override // You forgot this line 
    public void onReceive(Context context, Intent intent) 
    { 
      Toast.makeText(context, "Don't panik but your time is up!!!!.",Toast.LENGTH_LONG).show(); 
    } 
} 
0

您已經創建了一個startAlert函數其中U不叫anywhere.So第一次調用該方法中的onCreate和那麼你會收到一杯吐司。