2011-10-28 36 views
0

我使用AlarmManager在每個間隔調用一個活動,但是間隔必須可以更改。該活動只是另一個我已更改爲pendingIntent的函數。 調用類如下所示,它也是主類。被調用的類顯示在調用類之後。最後,清單文件可能包含錯誤的來源。無法啓動服務意圖:找不到

Timer_test: 

    package com.timer_test; 

    import android.app.Activity; 
    import android.app.AlarmManager; 
    import android.app.PendingIntent; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.os.SystemClock; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 

public class Timer_testActivity extends Activity { 
    /** Called when the activity is first created. */ 

    private PendingIntent mAlarmSender; 
    int update_freq = 1; 
    Intent toast_make; 
    int interval; 

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

    Button button1 = (Button)findViewById(R.id.button1); 
    Button button3 = (Button)findViewById(R.id.button3); 
    Button button5 = (Button)findViewById(R.id.button5); 
    Button button10 = (Button)findViewById(R.id.button10); 

    toast_make = new Intent("com.timer_test.toast_testClass"); 
    mAlarmSender = PendingIntent.getService(Timer_testActivity.this, 0, toast_make   ,0); 

    button1.setOnClickListener(new OnClickListener(){ 
     public void onClick(View arg0){ 
      update_freq = 1; 
      timer_schedule(); 
    } 
    }); 

    button3.setOnClickListener(new OnClickListener(){ 
     public void onClick(View arg0){ 
      update_freq = 3; 
      timer_schedule(); 
    } 
    }); 

    button5.setOnClickListener(new OnClickListener(){ 
     public void onClick(View arg0){ 
      update_freq = 5; 
      timer_schedule(); 
    } 
    }); 

    button10.setOnClickListener(new OnClickListener(){ 
     public void onClick(View arg0){ 
      update_freq = 10; 
      timer_schedule(); 
    } 
    }); 
} 

    public void timer_schedule(){ 

    long firstTime = SystemClock.elapsedRealtime(); 

    interval = update_freq*1000; 
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, interval, mAlarmSender); 

} 

package com.timer_test; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Toast; 

public class toast_test extends Activity{ 

public void onCreate(Bundle icicle){ 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    onClick("yes"); 
} 

public void onClick(String n) { 
    Toast.makeText(toast_test.this,n, 
     Toast.LENGTH_SHORT).show(); 
    } 

    } 


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

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".Timer_testActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <service android:name="com.timer_test.toast_test"/> 

    </application> 
</manifest> 

回答

1

toast_test是活動和表現,你宣佈它作爲服務

+0

true..However,我也跟着使用toast_test作爲的PendingIntent的例子,並與AlarmManager的getSystemService啓動它。我可以開始任何活動作爲意圖和使用AlarmManager? – user301

+0

我不清楚你在問什麼,但意圖就像活動之間傳遞的消息。您可以通過發送意向開始活動。您只需在清單文件中針對您的活動編寫intent過濾器。 – Vivek

相關問題