Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > Android เรื่อง Start Service ตอนเปิดเครื่องครับ ผมทำไม่เป็น



 

Android เรื่อง Start Service ตอนเปิดเครื่องครับ ผมทำไม่เป็น

 



Topic : 091661



โพสกระทู้ ( 13 )
บทความ ( 0 )



สถานะออฟไลน์




ตอนนี้ 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
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-03-01 15:08:54 By : VictoreD View : 3226 Reply : 6
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Quote:
start();


ก็น่าจะถูกแล้วน่ะครับ ลองใช้การ Debug ดูว่ามันไปหยุดตรงไหนครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-03-01 16:08:15 By : mr.win
 


 

No. 2



โพสกระทู้ ( 13 )
บทความ ( 0 )



สถานะออฟไลน์


แก้ได้แล้วครับ ใช้วิธีเรียกจาก 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
 

 

No. 3



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

เยี่ยมครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-03-01 16:28:04 By : mr.win
 


 

No. 4



โพสกระทู้ ( 13 )
บทความ ( 0 )



สถานะออฟไลน์


สวัสดีอีกครั้งครับ
ผมลองนำโค้ด 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
 


 

No. 5



โพสกระทู้ ( 13 )
บทความ ( 0 )



สถานะออฟไลน์


ไม่มีไรแล้วครับ แก้ได้แล้ว
error ตรงตั้งค่า default ครับ
จัดการลบออกหมดเลย
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-03-03 12:19:05 By : VictoreD
 


 

No. 6



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-03-03 16:06:19 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android เรื่อง Start Service ตอนเปิดเครื่องครับ ผมทำไม่เป็น
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่