如问题所述,以下显示mu代码..
package adham.test;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AlarmActivity extends Activity {
/** Called when the activity is first created. */
Button btnStart,btnStop;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart=(Button)this.findViewById(R.id.btnStart);
btnStop=(Button)this.findViewById(R.id.btnstop);
context = this.getApplicationContext();
startService(new Intent(context,AlarmService.class));
btnStart.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
startService(new Intent(context,AlarmService.class));
}
});
btnStop.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
stopService(new Intent(context,AlarmService.class));
}
});
}
}
package adham.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context c, Intent i) {
try{
Bundle b = i.getExtras();
String msg = b.getString("alarm_message");
Log.d("Receiverr ---","now");
Toast.makeText(c, msg, Toast.LENGTH_LONG).show();
}catch(Exception e){
}
}
}
package adham.test;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class AlarmService extends Service {
private static final String TAG = "MyService";
Context c;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
c= this;
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent i = new Intent(c,AlarmReceiver.class);
i.putExtra("alarm_message", "Hello, Alarm is running !");
PendingIntent pi= PendingIntent.getActivity(c, 19237, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)c.getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
}
@Override
public void onDestroy(){
Toast.makeText(this, "My Service Destroyed", Toast.LENGTH_LONG).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="adham.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestServicesActivity"
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=".AlarmService"></service>
<activity android:name=".AlarmActivity"></activity>
<receiver android:process=":remote" android:name=".AlarmReceiver"></receiver>
</application>
</manifest>
答案 0 :(得分:3)
我认为你从不提出任何广播。您的代码显示:
Intent i = new Intent(c,AlarmReceiver.class);
i.putExtra("alarm_message", "Hello, Alarm is running !");
PendingIntent pi= PendingIntent.getActivity(c, 19237, i, PendingIntent.FLAG_UPDATE_CURRENT);
您正在呼叫getActivity
,但AlarmReceiver
是Broadcast
,您需要拨打getBroadcast(...)
。