我研究了警报管理器和我自己的应用程序的自动启动。我想要一个不可见的服务(名称不正确,抱歉),它会定期向我展示敬酒(用于测试目的)。
以下代码运行但不正确。我点击“按钮”,活动消失但什么都没有......
我的代码:
MainActivity
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStart = (Button)findViewById(R.id.button1);
buttonStart.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
Intent myIntent = new Intent(getBaseContext(),
MyScheduledReceiver.class);
PendingIntent pendingIntent
= PendingIntent.getBroadcast(getBaseContext(),
0, myIntent, 0);
AlarmManager alarmManager
= (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = 60 * 10; //
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval, pendingIntent);
finish();
}});
}
}
AutoStartNotifyReceiver
public class AutoStartNotifyReceiver extends BroadcastReceiver {
private final String BOOT_COMPLETED_ACTION = "android.intent.action.BOOT_COMPLETED";
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(BOOT_COMPLETED_ACTION)){
Intent myIntent = new Intent(context, MyScheduledReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = 60 * 10;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
}
}
}
MyScheduledActivity
public class MyScheduledActivity extends Activity {
MediaPlayer player;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Toast.makeText(getApplicationContext(), "Eseguo onCreate", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
player = MediaPlayer.create(this, R.raw.gabriel);
player.start();
Toast.makeText(this, "miao",Toast.LENGTH_LONG).show();
}
}
MyScheduledReceiver
public class MyScheduledReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
}
显现:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".AutoStartNotifyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
{12-12 14:50:21.411: E/AndroidRuntime(865): FATAL EXCEPTION: main 12-12 14:50:21.411: E/AndroidRuntime(865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com .example.servi/com.example.servi.MyScheduledActivity}:java.lang.NullPointerException 12-12 14:50:21.411:E/AndroidRuntime(865):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 12-12 14:50:21.411: E/AndroidRuntime(865): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 12-12 14:50:21.411: E/AndroidRuntime(865): 在 android. app.ActivityThread.access$600(ActivityThread.java:123) 12-12 14:50:21.411: E/AndroidRuntime(865): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 12-12 14 :50:21.411: E/AndroidRuntime(865): 在 android.os.Handler.dispatchMessage(Handler.java:99) 12-12 14:50:21.411:E/AndroidRuntime(865): 在 android.os.Looper.loop(Looper.java:137) 12-12 14:50:21.411: E/AndroidRuntime(865): 在 android.app.ActivityThread.main(ActivityThread.java :4424) 12-12 14:50:21.411: E/AndroidRuntime(865): 在 java.lang.reflect.Method.invokeNative(Native Method) 12-12 14:50:21.411: E/AndroidRuntime(865): 在java.lang.reflect.Method.invoke(Method.java:511) 12-12 14:50:21.411: E/AndroidRuntime(865): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :784) 12-12 14:50:21.411: E/AndroidRuntime(865): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-12 14:50:21.411: E/AndroidRuntime (865): 在 dalvik.system.NativeStart.main(Native Method) 12-12 14:50:21.411: E/AndroidRuntime(865): 由: java.lang.NullPointerException 12-12 14:50:21.411: E /AndroidRuntime(865):在 com.example.servi。MyScheduledActivity.onCreate(MyScheduledActivity.java:22) 12-12 14:50:21.411: E/AndroidRuntime(865): 在 android.app.Activity.performCreate(Activity.java:4465) 12-12 14:50:21.411: E/AndroidRuntime(865): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-12 14:50:21.411: E/AndroidRuntime(865): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java :1920) 12-12 14:50:21.411: E/AndroidRuntime(865): ... 11 更多 }1920) 12-12 14:50:21.411: E/AndroidRuntime(865): ... 11 更多 }1920) 12-12 14:50:21.411: E/AndroidRuntime(865): ... 11 更多 }
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servi"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.servi.MainActivity"
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=".AutoStartNotifyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name="MyScheduledReceiver"></receiver>
<activity android:name="MyScheduledActivity"></activity>
</application>
</manifest>