hsiao 7 yıl önce
ebeveyn
işleme
2acfd87e4f

+ 1
- 0
lesson03/release/output.json Dosyayı Görüntüle

@@ -0,0 +1 @@
1
+[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":1},"path":"lesson03-release.apk","properties":{"packageId":"io.hsiao.lesson03","split":"","minSdkVersion":"23"}}]

BIN
lesson03/release/youmap.apk Dosyayı Görüntüle


+ 14
- 2
lesson03/src/main/AndroidManifest.xml Dosyayı Görüntüle

@@ -5,7 +5,7 @@
5 5
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
6 6
     <uses-permission android:name="android.permission.INTERNET" />
7 7
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
8
-    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
8
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
9 9
 
10 10
     <application
11 11
         android:allowBackup="true"
@@ -17,6 +17,7 @@
17 17
         <activity android:name=".MainActivity">
18 18
             <intent-filter>
19 19
                 <action android:name="android.intent.action.MAIN" />
20
+
20 21
                 <category android:name="android.intent.category.LAUNCHER" />
21 22
             </intent-filter>
22 23
         </activity>
@@ -24,7 +25,18 @@
24 25
             android:name=".PanoActivity"
25 26
             android:theme="@style/Theme.AppCompat.NoActionBar" />
26 27
         <activity android:name=".PanoVideoActivity" />
27
-        <activity android:name=".PublishActivity"></activity>
28
+        <activity android:name=".PublishActivity" />
29
+        <activity android:name=".ServiceTestActivity"></activity>
30
+
31
+        <service android:name=".job.NotifyService" android:permission="android.permission.BIND_JOB_SERVICE"></service>
32
+        <service android:name=".service.PullService"></service>
33
+        <service android:name=".service.PullService2"></service>
34
+
35
+        <!--<receiver android:name=".broadcast.MyReceiver">
36
+            <intent-filter >
37
+                <action android:name="android.intent.action.SCREEN_ON"></action>
38
+            </intent-filter>
39
+        </receiver>-->
28 40
     </application>
29 41
 
30 42
 </manifest>

BIN
lesson03/src/main/ic_launcher-web.png Dosyayı Görüntüle


+ 2
- 0
lesson03/src/main/java/io/hsiao/lesson03/MainActivity.java Dosyayı Görüntüle

@@ -158,6 +158,8 @@ public class MainActivity extends AppCompatActivity {
158 158
                 demoTask.setShouldFinish(false);
159 159
                 break;
160 160
             case R.id.menu_topic:
161
+                Intent intent1 = new Intent(this,ServiceTestActivity.class );
162
+                startActivity(intent1);
161 163
                 break;
162 164
             case R.id.menu_usercenter:
163 165
                 testPost();

+ 125
- 0
lesson03/src/main/java/io/hsiao/lesson03/ServiceTestActivity.java Dosyayı Görüntüle

@@ -0,0 +1,125 @@
1
+package io.hsiao.lesson03;
2
+
3
+import android.app.Service;
4
+import android.app.job.JobInfo;
5
+import android.app.job.JobParameters;
6
+import android.app.job.JobScheduler;
7
+import android.app.job.JobService;
8
+import android.content.ComponentName;
9
+import android.content.Context;
10
+import android.content.Intent;
11
+import android.content.IntentFilter;
12
+import android.support.v7.app.AppCompatActivity;
13
+import android.os.Bundle;
14
+import android.util.Log;
15
+import android.view.View;
16
+import android.widget.Button;
17
+
18
+import io.hsiao.lesson03.broadcast.MyReceiver;
19
+import io.hsiao.lesson03.job.NotifyService;
20
+import io.hsiao.lesson03.service.PullService;
21
+import io.hsiao.lesson03.service.PullService2;
22
+
23
+public class ServiceTestActivity extends AppCompatActivity {
24
+
25
+    private MyReceiver broadRev;
26
+
27
+    @Override
28
+    protected void onCreate(Bundle savedInstanceState) {
29
+        super.onCreate(savedInstanceState);
30
+        setContentView(R.layout.activity_service_test);
31
+
32
+        Button btnJobNetwork = (Button) findViewById(R.id.svc_test_btn_job_network  );
33
+        btnJobNetwork.setOnClickListener(new View.OnClickListener() {
34
+            @Override
35
+            public void onClick(View v) {
36
+                try {
37
+                    ComponentName svcComp = new ComponentName(ServiceTestActivity.this, NotifyService.class);
38
+                    JobInfo job = new JobInfo.Builder(100,svcComp)
39
+                            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
40
+                            //.setMinimumLatency(3000)
41
+                            //.setOverrideDeadline(10*1000)
42
+                            //.setRequiresCharging(false)
43
+                            .build();
44
+
45
+                    Log.i("notify","job "+job);
46
+                    JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
47
+                    int ret = scheduler.schedule(job);
48
+                    Log.i("notify","return "+ret);
49
+                } catch (Exception e) {
50
+                    e.printStackTrace();
51
+                }
52
+            }
53
+        });
54
+
55
+        Button btnSvcTest = (Button) findViewById(R.id.svc_test_btn_svc);
56
+        btnSvcTest.setOnClickListener(new View.OnClickListener() {
57
+            @Override
58
+            public void onClick(View v) {
59
+                try {
60
+                    Intent intent = new Intent(ServiceTestActivity.this,PullService.class);
61
+                    intent.putExtra("offset", 100);
62
+                    intent.putExtra("limit",10);
63
+                    intent.putExtra("sessionid","3wsersfsdf");
64
+
65
+                    startService(intent);
66
+                } catch (Exception e) {
67
+                    e.printStackTrace();
68
+                }
69
+                // startService(intent);
70
+               // startService(intent);
71
+            }
72
+        });
73
+
74
+        Button btnSvcTest2 = (Button) findViewById(R.id.svc_test_btn_svc2);
75
+        btnSvcTest2.setOnClickListener(new View.OnClickListener() {
76
+            @Override
77
+            public void onClick(View v) {
78
+                try {
79
+                    Intent intent = new Intent(ServiceTestActivity.this,PullService2.class);
80
+                    startService(intent);
81
+                } catch (Exception e) {
82
+                    e.printStackTrace();
83
+                }
84
+                // startService(intent);
85
+                // startService(intent);
86
+            }
87
+        });
88
+
89
+        Button btnSvcBroad = (Button) findViewById(R.id.svc_test_btn_broad);
90
+        btnSvcBroad.setOnClickListener(new View.OnClickListener() {
91
+            @Override
92
+            public void onClick(View v) {
93
+
94
+                Intent intent = new Intent();
95
+                intent.setAction("io.hsiao.lesson03.sayhello");
96
+
97
+                 sendBroadcast(intent);
98
+            }
99
+        });
100
+
101
+    }
102
+
103
+
104
+    @Override
105
+    protected void onResume() {
106
+        super.onResume();
107
+        // 1. 实例化BroadcastReceiver子类 &  IntentFilter
108
+          broadRev = new MyReceiver();
109
+        IntentFilter intentFilter = new IntentFilter();
110
+
111
+        // 2. 设置接收广播的类型
112
+        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
113
+        intentFilter.addAction("io.hsiao.lesson03.sayhello");
114
+
115
+        // 3. 动态注册:调用Context的registerReceiver()方法
116
+        registerReceiver(broadRev, intentFilter);
117
+
118
+    }
119
+
120
+    @Override
121
+    protected void onDestroy() {
122
+        super.onDestroy();
123
+        unregisterReceiver(broadRev);
124
+    }
125
+}

+ 25
- 0
lesson03/src/main/java/io/hsiao/lesson03/broadcast/MyReceiver.java Dosyayı Görüntüle

@@ -0,0 +1,25 @@
1
+package io.hsiao.lesson03.broadcast;
2
+
3
+import android.content.BroadcastReceiver;
4
+import android.content.Context;
5
+import android.content.Intent;
6
+import android.util.Log;
7
+import android.widget.Toast;
8
+
9
+import io.hsiao.lesson03.MainActivity;
10
+
11
+/**
12
+ * Created by hsiao on 2018/4/1.
13
+ */
14
+
15
+public class MyReceiver extends BroadcastReceiver {
16
+
17
+    @Override
18
+    public void onReceive(Context context, Intent intent) {
19
+        Log.i("broad rev","收到屏幕状态变化广播");
20
+        Toast.makeText(context,"屏幕亮了",Toast.LENGTH_SHORT).show();
21
+
22
+        Intent goIntent = new Intent(context,MainActivity.class);
23
+        context.startActivity(goIntent);
24
+    }
25
+}

+ 24
- 0
lesson03/src/main/java/io/hsiao/lesson03/job/NotifyService.java Dosyayı Görüntüle

@@ -0,0 +1,24 @@
1
+package io.hsiao.lesson03.job;
2
+
3
+import android.app.job.JobParameters;
4
+import android.app.job.JobService;
5
+import android.util.Log;
6
+import android.widget.Toast;
7
+
8
+public class NotifyService extends JobService {
9
+
10
+
11
+    @Override
12
+    public boolean onStartJob(JobParameters params) {
13
+        Log.i("notify","启动job");
14
+        Toast.makeText(this,"已经联网",Toast.LENGTH_SHORT).show();
15
+        return true;
16
+    }
17
+
18
+    @Override
19
+    public boolean onStopJob(JobParameters params) {
20
+        Log.i("notify","停止job");
21
+
22
+        return true;
23
+    }
24
+}

+ 119
- 0
lesson03/src/main/java/io/hsiao/lesson03/service/PullService.java Dosyayı Görüntüle

@@ -0,0 +1,119 @@
1
+package io.hsiao.lesson03.service;
2
+
3
+import android.app.Notification;
4
+import android.app.NotificationManager;
5
+import android.app.PendingIntent;
6
+import android.app.Service;
7
+import android.content.Context;
8
+import android.content.Intent;
9
+import android.graphics.BitmapFactory;
10
+import android.graphics.Color;
11
+import android.os.AsyncTask;
12
+import android.os.IBinder;
13
+import android.support.annotation.Nullable;
14
+import android.support.v4.app.NotificationCompat;
15
+import android.util.Log;
16
+import android.widget.RemoteViews;
17
+import android.widget.Toast;
18
+
19
+import com.alibaba.fastjson.JSONArray;
20
+
21
+import java.util.HashMap;
22
+
23
+import io.hsiao.lesson03.MainActivity;
24
+import io.hsiao.lesson03.R;
25
+import io.hsiao.lesson03.data.HttpAPI;
26
+import io.hsiao.lesson03.data.ImageApiClient;
27
+
28
+/**
29
+ * Created by hsiao on 2018/4/1.
30
+ */
31
+
32
+public class PullService extends Service{
33
+
34
+    private int callNb = 0;
35
+    private HttpAPI api;
36
+    private ImageApiClient imgApi;
37
+
38
+    private AsyncTask pullTask;
39
+    private boolean shouldPull = true;
40
+    private int notifyId = 0;
41
+    @Override
42
+    public void onCreate() {
43
+        super.onCreate();
44
+        Log.i("pull svc","create pull servcie");
45
+        api = new HttpAPI("http://reefun.cn/real360/api");
46
+        imgApi = new ImageApiClient();
47
+
48
+       this.pullTask =  new AsyncTask(){
49
+            @Override
50
+            protected Object doInBackground(Object[] objects) {
51
+
52
+                while(shouldPull) {
53
+                    try {
54
+                        Thread.sleep(3000);
55
+                    } catch (InterruptedException e) {
56
+                        e.printStackTrace();
57
+                    }
58
+
59
+                     String data = imgApi.postRaw("http://reefun.cn/youmap-service/notify.jsp",new HashMap<String, String>());
60
+                    Log.i("pull svc",data);
61
+                    if(data==null){
62
+                        continue;
63
+                    }
64
+
65
+                    publishProgress(data);
66
+
67
+                }
68
+                return null;
69
+            }
70
+
71
+           @Override
72
+           protected void onProgressUpdate(Object[] values) {
73
+                showNotification(++notifyId,values[0].toString());
74
+           }
75
+        };
76
+
77
+       this.pullTask.execute();
78
+    }
79
+
80
+    @Override
81
+    public int onStartCommand(Intent intent, int flags, int startId) {
82
+        callNb++;
83
+        Log.i("pull svc","nb "+callNb);
84
+
85
+        return super.onStartCommand(intent, flags, startId);
86
+    }
87
+
88
+    private void showNotification(int notifyId, String content){
89
+        Intent intent = new Intent(this, MainActivity.class);
90
+
91
+        PendingIntent pendingIntent = PendingIntent.getActivity(this,notifyId,intent,PendingIntent.FLAG_UPDATE_CURRENT);
92
+        //RemoteViews contentView =new RemoteViews();
93
+        Notification notify = new NotificationCompat.Builder(this)
94
+                .setSmallIcon(R.drawable.ic_launcher_foreground)
95
+                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))
96
+                .setContentTitle("您有新消息")
97
+                .setContentText(content)
98
+                .setTicker("友图新消息")
99
+                .setNumber(12)
100
+                .setAutoCancel(true)
101
+                .setContentIntent(pendingIntent)
102
+                .build();
103
+
104
+        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
105
+        mNotificationManager.notify(0, notify);
106
+    }
107
+
108
+    @Override
109
+    public void onDestroy() {
110
+        super.onDestroy();
111
+        Log.i("pull svc","pull service destroy");
112
+    }
113
+
114
+    @Nullable
115
+    @Override
116
+    public IBinder onBind(Intent intent) {
117
+        return null;
118
+    }
119
+}

+ 48
- 0
lesson03/src/main/java/io/hsiao/lesson03/service/PullService2.java Dosyayı Görüntüle

@@ -0,0 +1,48 @@
1
+package io.hsiao.lesson03.service;
2
+
3
+import android.app.IntentService;
4
+import android.content.ComponentCallbacks;
5
+import android.content.Intent;
6
+import android.content.res.Configuration;
7
+import android.support.annotation.Nullable;
8
+import android.util.Log;
9
+import android.widget.Toast;
10
+
11
+/**
12
+ * Created by hsiao on 2018/4/1.
13
+ */
14
+
15
+public class PullService2 extends IntentService {
16
+    public static final String NAME = "youmap_pull_servcie";
17
+    private int callNb = 0;
18
+
19
+    public PullService2() {
20
+        super(NAME);
21
+    }
22
+
23
+    @Override
24
+    public void onCreate() {
25
+        super.onCreate();
26
+        Log.i("pull svc2","create svc");
27
+    }
28
+
29
+    @Override
30
+    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
31
+        Log.i("pull svc2","exec cmd");
32
+        Toast.makeText(this,"intent service",Toast.LENGTH_SHORT).show();
33
+        return super.onStartCommand(intent, flags, startId);
34
+    }
35
+
36
+    @Override
37
+    protected void onHandleIntent(@Nullable Intent intent) {
38
+        Log.i("pull svc2","thread id "+Thread.currentThread().getId());
39
+        try {
40
+            Thread.sleep(3000);
41
+        } catch (InterruptedException e) {
42
+            e.printStackTrace();
43
+        }
44
+        callNb++;
45
+        Log.i("pull svc 2","call "+callNb);
46
+
47
+    }
48
+}

+ 56
- 0
lesson03/src/main/res/layout/activity_service_test.xml Dosyayı Görüntüle

@@ -0,0 +1,56 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+    xmlns:app="http://schemas.android.com/apk/res-auto"
4
+    xmlns:tools="http://schemas.android.com/tools"
5
+    android:layout_width="match_parent"
6
+    android:layout_height="match_parent"
7
+    tools:context="io.hsiao.lesson03.ServiceTestActivity">
8
+
9
+    <Button
10
+        android:id="@+id/svc_test_btn_job_network"
11
+        android:layout_width="0dp"
12
+        android:layout_height="wrap_content"
13
+        android:layout_marginEnd="8dp"
14
+        android:layout_marginStart="8dp"
15
+        android:layout_marginTop="8dp"
16
+        android:text="网络状态调度"
17
+        app:layout_constraintEnd_toEndOf="parent"
18
+        app:layout_constraintStart_toStartOf="parent"
19
+        app:layout_constraintTop_toTopOf="parent" />
20
+
21
+    <Button
22
+        android:id="@+id/svc_test_btn_svc"
23
+        android:layout_width="0dp"
24
+        android:layout_height="wrap_content"
25
+        android:layout_marginEnd="8dp"
26
+        android:layout_marginStart="8dp"
27
+        android:layout_marginTop="8dp"
28
+        android:text="测试后台服务"
29
+        app:layout_constraintEnd_toEndOf="parent"
30
+        app:layout_constraintStart_toStartOf="parent"
31
+        app:layout_constraintTop_toBottomOf="@+id/svc_test_btn_job_network" />
32
+
33
+    <Button
34
+        android:id="@+id/svc_test_btn_svc2"
35
+        android:layout_width="0dp"
36
+        android:layout_height="wrap_content"
37
+        android:layout_marginEnd="8dp"
38
+        android:layout_marginStart="8dp"
39
+        android:layout_marginTop="8dp"
40
+        android:text="测试后台服务(IntentService)"
41
+        app:layout_constraintEnd_toEndOf="parent"
42
+        app:layout_constraintStart_toStartOf="parent"
43
+        app:layout_constraintTop_toBottomOf="@+id/svc_test_btn_svc" />
44
+
45
+    <Button
46
+        android:id="@+id/svc_test_btn_broad"
47
+        android:layout_width="0dp"
48
+        android:layout_height="wrap_content"
49
+        android:layout_marginEnd="8dp"
50
+        android:layout_marginStart="8dp"
51
+        android:layout_marginTop="8dp"
52
+        android:text="测试广播发送"
53
+        app:layout_constraintEnd_toEndOf="parent"
54
+        app:layout_constraintStart_toStartOf="parent"
55
+        app:layout_constraintTop_toBottomOf="@+id/svc_test_btn_svc2" />
56
+</android.support.constraint.ConstraintLayout>

BIN
lesson03/src/main/res/mipmap-hdpi/ic_launcher.png Dosyayı Görüntüle


BIN
lesson03/src/main/res/mipmap-mdpi/ic_launcher.png Dosyayı Görüntüle


BIN
lesson03/src/main/res/mipmap-xhdpi/ic_launcher.png Dosyayı Görüntüle


BIN
lesson03/src/main/res/mipmap-xxhdpi/ic_launcher.png Dosyayı Görüntüle


BIN
lesson03/src/main/res/mipmap-xxxhdpi/ic_launcher.png Dosyayı Görüntüle


Loading…
İptal
Kaydet