描述
我正在开发一个应用程序(用于测试目的),每60秒将在数据库中保存一个单词(使用SQLiteOpenHelper)。我试图通过使用AlarmManager(setRepeating)+唤醒锁(PARTIAL_WAKE_LOCK)来实现这一点。即使屏幕是黑色,应用程序也运行很重要。
我测试了我的应用程序2天,尝试了几种方法,但我有1个问题,我无法弄清楚。我得到的代码最好的结果就是我发布的内容。
我在两款不同手机上测试了这款应用程序,三星Galaxy s6边缘和HTC One M8。在三星手机上,该应用程序将运行1-5次迭代,然后完全死亡。在HTC上,它将继续运行,就好像唤醒锁正常工作一样。在我的情况下,迭代是我的AlarmManager,它每60秒拨一次电话。
注意!该应用程序确实在三星手机上运行,如果我有连接充电的USB,但这是预期的,因为CPU不会睡觉。
我的问题
如果我想实现我的描述,我会做错事吗?
版本
三星Galaxy s6 Edge Android版:6.0.1
HTC One M8 Android版:6.0.1
代码
AndroidManifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Repeating_activity"/>
<receiver android:name=".Notification_receiver"/>
</application>
</manifest>
MainActivity
package com.example.acce.dailyrepeatinglocalnotification;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView text;
String tag = "LifeCycleEvents";
AlarmManager alarmManager;
PendingIntent pendingIntent;
boolean isAlarmRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "In the onCreate() event");
text = (TextView) findViewById(R.id.textView3);
}
public void startAlarm(View v) {
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),60000, pendingIntent);
Toast.makeText(this,"Alarm started", Toast.LENGTH_SHORT).show();
isAlarmRunning = true;
}
public void stopAlarm(View v) {
if(isAlarmRunning) {
alarmManager.cancel(pendingIntent);
Toast.makeText(this,"Alarm stopped", Toast.LENGTH_SHORT).show();
isAlarmRunning = false;
} else {
Toast.makeText(this,"No alarm running!", Toast.LENGTH_SHORT).show();
}
}
public void ShowAll(View view) {
List notificationList = new ArrayList<String>();
DatabaseHandler db = new DatabaseHandler(this);
notificationList = db.getAllNotifications();
int msg = notificationList.size();
text.setText(String.valueOf(msg));
}
}
Notification_receiver
package com.example.acce.dailyrepeatinglocalnotification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
public class Notification_receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "asdasdasd");
wl.acquire();
DatabaseHandler db = new DatabaseHandler(context);
db.addNotification("A new notification");
wl.release();
}
}
数据库处理器
package com.example.acce.dailyrepeatinglocalnotification;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Daniel on 01-10-2016.
*/
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "AlarmNotifications";
// table name
private static final String TABLE_NAME = "notifications";
// Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
// add
public void addNotification(String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
db.insert(TABLE_NAME, null, values);
db.close();
}
public List getAllNotifications() {
List<String> notifcationList = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.moveToFirst()) {
do {
notifcationList.add(cursor.getString(1)); // the name
} while(cursor.moveToNext());
}
return notifcationList;
}
}