Android เรื่อง Start Service ตอนเปิดเครื่องครับ ผมทำไม่เป็น
ตอนนี้ service ผมรันได้แล้ว แต่ถ้าปิดเครื่องแล้วเปิดใหม่ service จะไม่ทำงาน
ผมต้องการให้ service ทำงาน ทำยังไงดีครับ
นี่โค้ดครับ :
Code
AndroidManifest (Java)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.applicationaccount"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
<activity
android:name="com.example.applicationaccount.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>
<service
android:name=".NotificationService"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</service>
</application>
</manifest>
NotificationService (Java)
package com.example.applicationaccount;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class NotificationService extends Service {
//Variable
//Variable not null
NotificationBroadcast notic = new NotificationBroadcast();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
start();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(notic);
Toast.makeText(getApplication(),"Stop services..", Toast.LENGTH_LONG).show();
super.onDestroy();
}
private void start(){
IntentFilter intentfilter = new IntentFilter(Intent.ACTION_TIME_TICK);
intentfilter.addAction(Intent.ACTION_PACKAGE_ADDED);
registerReceiver(notic, intentfilter);
Log.d("#", "services start");
Toast.makeText(getApplication(),"Start services..", Toast.LENGTH_LONG).show();
}
}
NotificationBroadcast (Android-Java)
package com.example.applicationaccount;
import java.util.Calendar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class NotificationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(Intent.ACTION_TIME_TICK)||action.equals(Intent.ACTION_BOOT_COMPLETED)){
switch (Calendar.getInstance().get(Calendar.MINUTE)) {
case 5:
case 10:
case 15:
case 20:
case 25:
case 30:
case 35:
case 40:
case 45:
case 50:
case 55:
case 0:
Toast.makeText(context,"ทำแล้วววววว", Toast.LENGTH_LONG).show();
Log.d("broad", "ทำแล้ว");
break;
default:
Toast.makeText(context,"หยุดทำทำแล้วววววว", Toast.LENGTH_LONG).show();
Log.d("broad", "หยุดทำแล้ว");
break;
}
}
}
}
Tag : Mobile
ประวัติการแก้ไข 2013-03-01 15:25:49 2013-03-01 15:26:35 2013-03-01 15:39:10 2013-03-01 16:10:49
Date :
2013-03-01 15:08:54
By :
VictoreD
View :
3329
Reply :
6
แก้ได้แล้วครับ ใช้วิธีเรียกจาก BroadcastReceiver ให้ไปเปิด Services อีกทีนึงครับ
AndroidManifest (Android-Java)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.applicationaccount"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme" >
<activity
android:name="com.example.applicationaccount.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>
<service android:name=".NotificationService"></service>
<receiver android:name=".NotificationBroadcast"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
NotificationBroadcast (Android-Java)
package com.example.applicationaccount;
import java.util.Calendar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class NotificationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(Intent.ACTION_TIME_TICK)){
switch (Calendar.getInstance().get(Calendar.MINUTE)) {
case 5:
case 10:
case 15:
case 20:
case 25:
case 30:
case 35:
case 40:
case 45:
case 50:
case 55:
case 0:
Toast.makeText(context,"ทำแล้วววววว", Toast.LENGTH_LONG).show();
Log.d("broad", "ทำแล้ว");
break;
default:
Toast.makeText(context,"หยุดทำทำแล้วววววว", Toast.LENGTH_LONG).show();
Log.d("broad", "หยุดทำแล้ว");
break;
}
}
if(action.equals(Intent.ACTION_BOOT_COMPLETED)){
Intent intents = new Intent(context,NotificationService.class);
context.startService(intents);
}
}
}
Date :
2013-03-01 16:15:30
By :
VictoreD
เยี่ยมครับ
Date :
2013-03-01 16:28:04
By :
mr.win
สวัสดีอีกครั้งครับ
ผมลองนำโค้ด Notification ของ Thaicreate มาใช้กับ BroadcastRevicer มันแสดง Notification บน StatusBar ถูกต้องทุกอย่างครับ แต่ติดตรง BroadcastRevicer lifecycle นี่แหละมั๊งครับ(ไปหาจาก stackoverflow มา) ตอน Debug ก็ติดตรงบรรทัด 43 เลยอยากให้ช่วยดูให้หน่อยครับว่าเกิดจากอะไร หาวิธีแก้จนหัวฟูแล้ว - -
ต่อจากโค้ดเดิมเลยนะครับ
NotificationBroadcast (Android-Java)
package com.example.applicationaccount;
import java.util.Calendar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class NotificationBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(Intent.ACTION_TIME_TICK)){
createNotification(context,"เตือน","ข้อความทดสอบ");
}
if(action.equals(Intent.ACTION_BOOT_COMPLETED)){
Intent intents = new Intent(context,NotificationService.class);
context.startService(intents);
}
}
}
private void createNotification(Context context,String title,String msg){
NotificationManager noticeMng = (NotificationManager) context.getSystemService
(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context,TransactionActivity.class);
PendingIntent activity = PendingIntent.getActivity(context, 0, intent, 0);
Notification notice = new Notification(android.R.drawable.btn_star_big_on,
"New Notification",
System.currentTimeMillis());
notice.setLatestEventInfo(context, title, msg, activity);
notice.flags |= Notification.FLAG_AUTO_CANCEL;
notice.defaults = Notification.DEFAULT_SOUND;
notice.defaults = Notification.DEFAULT_VIBRATE;
notice.number += 1;
noticeMng.notify(1,notice);
}
อันนี้คือ Log
Code
03-31 20:56:00.259: E/AndroidRuntime(16002): FATAL EXCEPTION: main
03-31 20:56:00.259: E/AndroidRuntime(16002): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.TIME_TICK flg=0x40000004 (has extras) } in com.example.applicationaccount.NotificationBroadcast@40596bc8
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.os.Handler.handleCallback(Handler.java:587)
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.os.Handler.dispatchMessage(Handler.java:92)
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.os.Looper.loop(Looper.java:130)
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-31 20:56:00.259: E/AndroidRuntime(16002): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 20:56:00.259: E/AndroidRuntime(16002): at java.lang.reflect.Method.invoke(Method.java:507)
03-31 20:56:00.259: E/AndroidRuntime(16002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-31 20:56:00.259: E/AndroidRuntime(16002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-31 20:56:00.259: E/AndroidRuntime(16002): at dalvik.system.NativeStart.main(Native Method)
03-31 20:56:00.259: E/AndroidRuntime(16002): Caused by: java.lang.SecurityException: Requires VIBRATE permission
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.os.Parcel.readException(Parcel.java:1322)
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.os.Parcel.readException(Parcel.java:1276)
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:322)
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.app.NotificationManager.notify(NotificationManager.java:111)
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.app.NotificationManager.notify(NotificationManager.java:91)
03-31 20:56:00.259: E/AndroidRuntime(16002): at com.example.applicationaccount.NotificationBroadcast.createNotification(NotificationBroadcast.java:50)
03-31 20:56:00.259: E/AndroidRuntime(16002): at com.example.applicationaccount.NotificationBroadcast.onReceive(NotificationBroadcast.java:31)
03-31 20:56:00.259: E/AndroidRuntime(16002): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
03-31 20:56:00.259: E/AndroidRuntime(16002): ... 9 more
ประวัติการแก้ไข 2013-03-02 21:05:17 2013-03-02 21:06:34
Date :
2013-03-02 21:03:45
By :
VictoreD
ไม่มีไรแล้วครับ แก้ได้แล้ว
error ตรงตั้งค่า default ครับ
จัดการลบออกหมดเลย
Date :
2013-03-03 12:19:05
By :
VictoreD
Date :
2013-03-03 16:06:19
By :
mr.win
Load balance : Server 05