单击按钮后,我必须在10 sec
之后安排在设备上调用已安装的测试项目。为此,我创建了一个AlarmReceiver
和我的TaskService
,它实际上正在调用测试项目。
运行应用程序后,单击该按钮后没有任何反应。我不知道出了什么问题,但服务即使在10秒后也无法完成工作。
以下是我正在尝试的代码。 Activity类:
// Create an anonymous implementation of OnClickListener
private OnClickListener HTListener = new OnClickListener() {
public void onClick(View v) {
btnStartSchedule(v);
}
};
public void btnStartSchedule(View v) {
try {
AlarmManager alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
intent.putExtra(AlarmReceiver.ACTION_ALARM, AlarmReceiver.ACTION_ALARM);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pIntent);
Toast.makeText(ScrapeActivity.this, "Alarm Started", Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
AlarmReceiver
类:
public class AlarmReceiver extends BroadcastReceiver {
public static String ACTION_ALARM = "com.alarmmanager.alarm";
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Alarm Receiver", "Entered");
Toast.makeText(context, "Entered", Toast.LENGTH_SHORT).show();
Bundle bundle = intent.getExtras();
String action = bundle.getString(ACTION_ALARM);
if (action.equals(ACTION_ALARM)) {
Log.i("Alarm Receiver", "If loop");
Intent inService = new Intent(context, LaunchService.class);
context.startService(inService);
} else {
Log.i("Alarm Receiver", "Else loop");
Toast.makeText(context, "Else loop", Toast.LENGTH_SHORT).show();
}
}
}
最后是LaunchService
类:
public class LaunchService extends IntentService {
public LaunchService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.my.testproject");
startActivity(LaunchIntent);
}
}
我还提到了清单文件中的接收者和服务:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.pb.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/PB_APP"
android:theme="@style/AppTheme" >
<activity
android:name="com.my.pb.activity.ScrapeActivity"
android:label="@string/PB_APP" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AlarmReceiver"
android:process=":remote" >
</receiver>
<service android:name=".LaunchService" >
</service>
</application>
我不明白这个问题是什么,因为Logcat中没有例外。当我点击按钮时,我只看到吐司留言Alarm Started
。请帮忙。
答案 0 :(得分:3)
您在Manifest
处输入了一个拼写错误,您在那里声明了AlaramReceiver
而不是.AlarmReceiver
。